POST一些数据时WCF错误400错误请求

时间:2011-03-08 17:35:22

标签: .net wcf

我的本​​地计算机上有一个WCF服务,就像客户端应用程序和远程数据库之间的“桥梁”一样。

WCF服务与Entity Framework类一起使用,在获取数据时工作正常,但在发布任何内容时不起作用,我收到下一条错误消息:“(400)Bad Request”。

这是我的客户代码:

//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)。 任何的想法?

正如您所看到的,这是一个简单的例子,但不起作用:(

谢谢你的帮助! :)

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:-1)

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

$user->settings()->first()->something

以下更改是我在WCF web.config文件中完成的(在下面的配置中包括lessthen和greatthen符号,因为由于标签的不可见而无法添加)

发件人:

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

收件人


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

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

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

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

谢谢, 阿玛拉吉(M.Amalraj)