gpt4 book ai didi

.net - 调用 WCF 服务操作时出现 HTTP 400 错误请求?

转载 作者:行者123 更新时间:2023-12-04 15:15:15 24 4
gpt4 key购买 nike

当我打电话时http://localhost/TestService.svc/GetColors , 我收到一个 Http (400) 错误请求。当我在 fiddler 中运行它时,我也会收到错误的请求。当我通过 wcf 测试客户端调用服务时,它工作正常。什么可能导致这种情况?

服务合约:

[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetColors();
}

ITestService 的实现:
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class TestService : ITestService
{
public List<Color> GetColors()
{
List<Color> colors= new List<Color>();

colors.Add(new Color { Name = "Red", Code = "123" });
colors.Add(new Color { Name = "Blue", Code = "323" });
colors.Add(new Color { Name = "Green", Code = "3394" });

return colors;
}
}

这是我的 web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="CustomAuthentication">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<services>
<service name="TestService.TestService"
behaviorConfiguration="CustomValidator" >
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="CustomAuthentication"
contract="TestService.ITestService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/TestService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CustomValidator">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="TestService.CustomUserNameValidator, TestService"/>
<serviceCertificate findValue="Test" storeLocation="LocalMachine"
storeName="My" x509FindType="FindBySubjectName"/>
</serviceCredentials>
<serviceMetadata httpGetEnabled="True"/>
</behavior>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

当我调用它时,我只需打开任何浏览器并输入 url http://localhost/TestService.svc/GetColors .如果我通过开发环境执行此操作,则会看到 Http 400 错误请求。如果我通过 IIS 执行此操作,我只会看到一个空白页面。

这是内部异常:
<ExceptionString>System.Xml.XmlException: The body of the message cannot be read  
because it is empty.</ExceptionString>

关于我的 CustomUserNameValidation 的另一个问题:

我正在通过 UserNamePasswordValidator 的 Validate 方法实现自定义验证,但是当我通过 wcf 客户端调用 GetColors 之类的东西时,它不会调用 Validate 方法。我可以让它调用验证的唯一方法是,如果我直接在 GetColors 中调用 Validate(user,pass)。我认为如果您在 web.config 中正确设置了它,它将为每个服务调用自动调用。

最佳答案

您的服务契约(Contract)和服务实现似乎不匹配....

服务契约(Contract)定义了一个 stringGetColors() 返回:

[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetColors();
}

但实现返回 List<Color>反而:
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class TestService : ITestService
{
public List<Color> GetColors()
{

有没有可能你改变了你的服务实现,忘记更新服务契约(Contract)?

另外:因为这是一个带有 wsHttpBinding 的 SOAP 服务,你 不能 只需通过浏览到其地址来从浏览器调用该方法 - 您需要使用 WCF 测试客户端(如您所说,它可以工作)。您也不能仅从 Fiddler 调用它——您必须手动创建整个 SOAP 信封,包括 header 和正文以及所有内容——这根本不是一件容易的事。

你可以尝试的是去 http://localhost/TestService.svc?wsdl检查您是否会为该服务取回正确的 WSDL。

关于.net - 调用 WCF 服务操作时出现 HTTP 400 错误请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5995425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com