【问题标题】:HTTP 400 Bad Request when calling WCF Service Operation?调用 WCF 服务操作时出现 HTTP 400 错误请求?
【发布时间】:2011-08-25 02:57:55
【问题描述】:

当我调用http://localhost/TestService.svc/GetColors 时,我收到一个 Http (400) 错误请求。当我在提琴手中运行它时,我也会收到错误的请求。当我通过 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 bad request。如果我通过 IIS 执行此操作,我只会看到一个空白页。

这里是 InnerException:

<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 中正确设置了它,那么每次服务调用都会自动调用它。

【问题讨论】:

  • @marc_s,我会更新我的帖子。

标签: .net wcf bad-request


【解决方案1】:

您的服务合同和服务实施似乎不匹配....

服务契约定义了一个从GetColors()返回的string

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

但实现返回一个 List&lt;Color&gt; 代替:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class TestService : ITestService
{
    public List<Color> GetColors()
    {

您是否有可能更改了服务实现,却忘记更新服务合同??

另外:由于这是一个带有wsHttpBinding 的 SOAP 服务,您不能只是通过浏览器的地址从浏览器调用该方法 - 您需要使用 WCF 测试客户端(它有效, 正如你所说)。您也不能只从 Fiddler 调用它 - 您必须手动创建带有标题和正文的整个 SOAP 信封 - 这根本不是一件容易的事。

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

【讨论】:

  • 对不起,marc_s,我只是忘了在这里编辑它。我的实际代码是 List GetColors();在我的界面中。
  • marc_s,感谢您迄今为止的帮助。我确实得到了正确的 wsdl。您对用户名密码验证器问题有任何想法吗?
  • 使用SoapUI进行测试,它会为您创建空请求,然后您填写参数并发送它
【解决方案2】:

很多事情都可能导致这种情况。我建议您首先启用 WCF 跟踪,它通常很有帮助,因为它可以为您提供服务器端的实际情况输出。这是一个应该有帮助的链接:How to enable WCF tracing

编辑:使用 WCF 需要注意的一点:与自动生成的 ole' ASMX Web 服务不同,它不允许您使用标准浏览器浏览到基地址.您可以浏览到元数据地址(mex 之一),但不能浏览到根。这只是设计使然(我相信不支持 HTTP 方法)。

【讨论】:

  • 好的,我启用了跟踪。我现在如何从中获得有意义的东西?
  • 您需要使用服务跟踪查看器工具:msdn.microsoft.com/en-us/library/ms732023.aspx
  • 我发现了,但我似乎看不到任何问题。我没有看到任何错误图标。有什么需要注意的吗?
  • @xaisoft - 我已经更新了我的答案。我认为您正在尝试浏览基地址。你就是不能那样做。
  • 西蒙,你是对的。我正在尝试浏览到基地址\GetColors。那么这是否意味着一切实际上都在按预期工作,并且当我尝试通过浏览器 url 调用操作 (GetColors) 时,该错误实际上是预期的。
猜你喜欢
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
相关资源
最近更新 更多