Xai*_*oft 6 .net wcf bad-request
当我调用http://localhost/TestService.svc/GetColors时,我得到一个Http(400)错误请求.当我在小提琴手中运行时,我也得到了错误请求.当我通过wcf测试客户端调用服务时,它工作正常.什么可能导致这个?
服务合约:
[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetColors();
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
当我打电话给它时,我只是打开一个浏览器并输入网址http://localhost/TestService.svc/GetColors.如果我通过开发环境这样做,我会看到Http 400错误的请求.如果我通过IIS执行此操作,我只会看到一个空白页面.
这是InnerException:
<ExceptionString>System.Xml.XmlException: The body of the message cannot be read
because it is empty.</ExceptionString>
Run Code Online (Sandbox Code Playgroud)
关于我的CustomUserNameValidation的另一个问题:
我通过UserNamePasswordValidator的验证方法实现自定义验证,但是当我通过wcf客户端调用类似GetColors的东西时,它不会调用Validate方法.我可以将它调用Invoke验证的唯一方法是在GetColors中直接调用Validate(user,pass).如果你在web.config中正确设置它,我认为它可以自动调用每个服务调用.
您的服务合同和服务实施似乎不匹配....
服务合同定义了string从GetColors()以下位置返回的单个:
[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetColors();
}
Run Code Online (Sandbox Code Playgroud)
但是实现返回了一个List<Color>:
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class TestService : ITestService
{
public List<Color> GetColors()
{
Run Code Online (Sandbox Code Playgroud)
你有没有机会改变你的服务实现,忘了更新服务合同?
另外:由于这是一个带有a的SOAP服务wsHttpBinding,你不能只通过浏览到它的地址从浏览器调用该方法 - 你需要使用WCF测试客户端(如你所说的那样工作).你也不能从Fiddler那里调用它 - 你必须手动创建带有标题和正文的整个SOAP信封 - 这根本不是一件容易的事.
您可以尝试的是去http://localhost/TestService.svc?wsdl检查您是否将获得该服务的正确WSDL.
| 归档时间: |
|
| 查看次数: |
30983 次 |
| 最近记录: |