WCF - 远程服务器返回意外响应:(400)错误请求

Posted

技术标签:

【中文标题】WCF - 远程服务器返回意外响应:(400)错误请求【英文标题】:WCF - The remote server returned an unexpected response: (400) Bad Request 【发布时间】:2011-02-15 07:58:32 【问题描述】:

我在将 byte[] 传递给 WCF 时收到此错误。有人可以解决这个错误吗??

服务配置(web.config)

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    </system.web>
    <system.serviceModel>
        <bindings>
   <basicHttpBinding>
    <binding name="NewBinding0" maxBufferSize="2097151" maxBufferPoolSize="2097151"
     maxReceivedMessageSize="2097151" messageEncoding="Mtom"
     transferMode="Streamed">
     <readerQuotas maxDepth="2097151" maxStringContentLength="2097151"
      maxArrayLength="2097151" maxBytesPerRead="2097151" maxNameTableCharCount="2097151" />
    </binding>
   </basicHttpBinding>
   <mexHttpBinding>
    <binding name="higherMessageSize_MEX" />
   </mexHttpBinding>
  </bindings>
  <client>
   <endpoint binding="basicHttpBinding" bindingConfiguration="NewBinding0"
    contract="LService.IService">
    <identity>
     <dns value="localhost" />
    </identity>
   </endpoint>
   <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="higherMessageSize_MEX"
    contract="IMetadataExchange" />
  </client>
  <behaviors>
            <serviceBehaviors>
                <behavior>
                    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                    <serviceMetadata httpGetEnabled="true"/>
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

WPF 应用程序的配置 (app.config)

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="2097151" maxBufferPoolSize="2097151" maxReceivedMessageSize="2097151"
                    messageEncoding="Mtom" textEncoding="utf-8" transferMode="Streamed"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="2097151" maxStringContentLength="2097151"
                        maxArrayLength="2097151" maxBytesPerRead="2097151" maxNameTableCharCount="2097151" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:5980/LService/Service.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
                contract="LService.IService" name="BasicHttpBinding_IService" />
        </client>
    </system.serviceModel>
</configuration>

【问题讨论】:

如果您发布代码、XML 或数据示例,在文本编辑器中突出显示这些行,然后单击编辑器工具栏上的“代码示例”按钮 ()很好地格式化和语法高亮它! 您的服务合同是什么样的??您如何从客户那里调用该服务?您是如何创建客户端代理的(svcutil?添加服务引用??) 【参考方案1】:

在不知道您的服务合同和所需的所有细节的情况下 - 我只能推测。

让我印象深刻的是,您的服务器端配置不包含任何&lt;service&gt; 的配置。

规则是这样的:

服务器端,您的配置中需要一个&lt;services&gt; 标签,其中包含任意数量的&lt;service&gt; 标签,用于定义该服务器上的每个服务。每个&lt;service&gt; 标签又可以包含任意数量的&lt;endpoint&gt; 标签来定义一个或多个服务端点

客户端上,您有一个或多个&lt;client&gt; 条目,每个条目都包含一个&lt;endpoint&gt;,用于定义您的客户端连接到的服务地址

所以你的服务器端配置应该是这样的:

<system.serviceModel>
   <bindings>
      <basicHttpBinding>
          <binding name="NewBinding0" ....... />
      </basicHttpBinding>
      .....
   </bindings>
   <services>
       <service name="YourNamespace.YourService"
                behaviorConfiguration="Default" >
           <endpoint name="BasicEndpoint"
               address="http://localhost:5757/Services"
               binding="basicHttpBinding"
               bindingConfiguration="NewBinding0"
               contract="YourNamespace.IYourServiceContract" />
       </service>
   </services>
   <behaviors>
      <serviceBehaviors>
         <behavior name="Default">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="false"/>
         </behavior>
      </serviceBehaviors>
   </behaviors>
   ......
</system.serviceModel>

您的客户端配置看起来不错 - 当然,您必须将服务所在的服务器地址放入 address= 属性中 - 通常不是 localhost

【讨论】:

以上是关于WCF - 远程服务器返回意外响应:(400)错误请求的主要内容,如果未能解决你的问题,请参考以下文章

远程服务器返回了意外响应:(400) Bad Request WCF REST

“远程服务器返回意外响应:(400)错误请求”

远程服务器返回意外响应:(400) Bad Request

远程服务器返回意外响应:(400) Bad Request

远程服务器返回意外响应:(413) 请求实体太大。

向 WCF 发送集合时,远程服务器返回错误:NotFound