【问题标题】:WCF Error 400 Bad Request when POST some dataPOST 某些数据时出现 WCF 错误 400 错误请求
【发布时间】:2011-03-08 17:35:22
【问题描述】:

我的本​​地计算机上有一个 WCF 服务,它的工作方式类似于客户端应用程序和远程数据库之间的“桥梁”。

WCF 服务与实体框架类一起工作,在获取数据时工作正常,但在发布任何内容时不起作用,我收到下一条错误消息:“(400) 错误请求”。

这是我的客户端代码:

//Connect to WCF Service
CHS_Local.ServiceFrontalClient proxy = new CHS_Local.ServiceFrontalClient();

//Get a "Client" class wich ClientID == 1
CHS_Local.Client client = proxy.GetClient(1);

//Change some stuff
client.Name = "change someting";

//Send the modified class to service to update the database
proxy.UpdateClient(client);

这是我在 wcf 配置文件中的 <system.serviceModel> 标签:

<system.serviceModel>
   <services>
      <service name="CentralizedHostingService.ServiceFrontal">
         <endpoint 
             address="" 
             binding="wsHttpBinding" 
             contract="CentralizedHostingService.IServiceFrontal">
            <identity>
               <dns value="localhost" />
            </identity>
         </endpoint>
         <endpoint 
             address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
         <host>
            <baseAddresses>
               <add baseAddress="http://localhost:8732/Design_Time_Addresses/CentralizedHostingService/Service1/" />
            </baseAddresses>
         </host>
     </service>
  </services>
  <behaviors>
    <serviceBehaviors>
       <behavior>
          <serviceMetadata httpGetEnabled="True" />
         <serviceDebug includeExceptionDetailInFaults="False" />
       </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

还有我在客户端应用程序中的app.config

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IServiceFrontal" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" 
                hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="5000000"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas 
                   maxDepth="32" maxStringContentLength="8192" 
                   maxArrayLength="16384" maxBytesPerRead="4096" 
                   maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" 
                               proxyCredentialType="None" realm="" />
                    <message clientCredentialType="Windows" 
                             negotiateServiceCredential="true"
                             algorithmSuite="Default" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint 
            address="http://localhost:8732/Design_Time_Addresses/CentralizedHostingService/Service1/"
            binding="wsHttpBinding" 
            bindingConfiguration="WSHttpBinding_IServiceFrontal"
            contract="CHS_Local.IServiceFrontal" name="WSHttpBinding_IServiceFrontal">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

服务接口:

    [OperationContract]
    Client GetClient(int clientID);

    [OperationContract]
    void UpdateClient(Client editedClient);

在第一个例子中,我认为问题在于请愿书的重量,但我看到使用 Fiddler 为请愿书发送的字节只有 115.903 字节(~0.11MB)。 任何想法?

如您所见,这是一个简单的示例,但不起作用:(

感谢您的帮助! :)

【问题讨论】:

  • 把你的服务接口放在这里

标签: .net wcf


【解决方案1】:

从客户端发布数据时,您必须在服务上设置maxReceivedMessageSizereaderQuotas(如果需要)。您的客户端配置已修改maxReceivedMessageSize,它适用于从服务获取数据,但是当您向服务发送大量数据时,您也必须修改其配置。

类似:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="myBinding" maxReceivedMessageSize="500000" />
    </wsHttpBinding>
  </bindings>
  <services>
    <service name="CentralizedHostingService.ServiceFrontal">
      <endpoint bindingConfiguration="myBinding" ... />
      ...
    </service>
  </services>
  ...
</system.serviceModel>

【讨论】:

  • 我已经更新了客户端和服务器的配置。但错误仍然存​​在。
【解决方案2】:

当我将通过线路发送更多字节时,我在暂存环境中也遇到了这个问题。

The remote server returned an unexpected response: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.

我在 WCF web.config 文件中进行了以下更改(在以下配置中包含 lessthen 和 greaterthen 符号,因为由于标签不可见,我无法添加)

发件人:


 binding name="ServiceBinding" maxReceivedMessageSize="**262144**"

 binding name="ServiceBinding" maxReceivedMessageSize="**2147483647**"

下面是上面绑定的终点地址标签

 endpoint address="" binding="basicHttpBinding" bindingConfiguration="**ServiceBinding**"

经过上述更改后,它就像魅力一样。

谢谢, M·阿玛拉杰

【讨论】:

    猜你喜欢
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多