我在本地计算机上有一个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)。任何想法?
如您所见,这是一个简单的示例,但不起作用:(
感谢您的帮助! :)
从客户端发布数据时,必须在服务上设置maxReceivedMessageSize
和readerQuotas
(如果需要)。您的客户端配置已修改maxReceivedMessageSize
,可用于从服务获取数据,但是当您向服务发送大量数据时,也必须修改其配置。
类似:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="myBinding" maxReceivedMessageSize="500000" />
</wsHttpBinding>
</bindings>
<services>
<service name="CentralizedHostingService.ServiceFrontal">
<endpoint bindingConfiguration="myBinding" ... />
...
</service>
</services>
...
</system.serviceModel>
我在暂存环境中也将遇到此问题,当我将通过网络发送更多字节时。远程服务器返回了意外的响应:(400)错误的请求。 ---> System.Net.WebException:远程服务器返回错误:(400)错误的请求。
下面的更改是我在WCF web.config文件中完成的(在下面的配置中包括lessthen和greatthen符号,因为由于标签的不可见而无法添加)
发件人:
binding name =“ ServiceBinding” maxReceivedMessageSize =“ 262144”
收件人
binding name =“ ServiceBinding” maxReceivedMessageSize =“ 2147483647”
以下是以上绑定的终点地址标签
端点地址=“” binding =“ basicHttpBinding” bindingConfiguration =“ ServiceBinding”
经过上述更改之后,它就像魅力一样工作。
谢谢,M.Amalraj