.net POST 一些数据时出现 WCF 错误 400 错误请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5236132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:21:32  来源:igfitidea点击:

WCF Error 400 Bad Request when POST some data

.netwcf

提问by vfportero

I have a WCF Service in my local machine that works like a "bridge" between a client application and a database in remote.

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

The WCF service works with Entity Framework classes and works fine when get the data but doesn't work when post anything and i getting the next error message: "(400) Bad Request".

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

This is my client code:

这是我的客户端代码:

//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);

This my <system.serviceModel>tag in wcf config file:

<system.serviceModel>是 wcf 配置文件中的my标记:

<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>

And my app.configin client application:

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>

Service Interface:

服务接口:

    [OperationContract]
    Client GetClient(int clientID);

    [OperationContract]
    void UpdateClient(Client editedClient);

In a first instance I though that the problem resides in the weight of the petition but i see using Fiddler that bytes send for the petition are only 115.903 bytes (~0.11MB). Any idea?

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

As you can see is a simple example but don't works :(

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

Thanks for you help! :)

谢谢你的帮助!:)

采纳答案by Ladislav Mrnka

You must set maxReceivedMessageSizeand readerQuotas(if needed) on the service when posting data from client. Your client configuration has modified maxReceivedMessageSizewhich works for getting data from service but when you send large amount of data to the service you must modify its configuration as well.

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

Something like:

就像是:

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

回答by Amal Raj

I also faced this issue in my staging environment when i will send more bytes over the wire.

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

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

below changes i made it in WCF web.config file(include lessthen and greaterthen symbol in the below configuration since i could not add due to invisible of the tag)

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

From:

从:


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

To

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

Below is the end point address tag for above binding

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

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

After above changes then it works like charm.

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

Thanks, M.Amalraj

谢谢,M.Amalraj