我正在从某个 stub 调用 WCF 服务。问题是,我无法在客户端拥有 app.config
。因此,我在代码中设置值。
服务 web.config
显示如下值:
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IStatementsManagerService" openTimeout="00:10:00"
closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="true"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="567890" maxBufferPoolSize="524288" maxReceivedMessageSize="567890"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<!--<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>-->
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<services>
<service name="PathFINDER.Services.IStatementsManagerService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IStatementsManagerService"
contract="PopulateReqProServiceERReqs.IStatementsManagerService"
name="BasicHttpBinding_IStatementsManagerService" />
<endpoint kind="mexEndpoint" address="mex" />
</service>
</services>
</system.serviceModel>
而客户端的代码是这样的:
BasicHttpBinding binding = new BasicHttpBinding();
//System.ServiceModel.Channels.Binding binding = new BasicHttpBinding();
TimeSpan t = new TimeSpan(0, 2, 0);
binding.Name = "BasicHttpBinding_IStatementsManagerService";
binding.CloseTimeout = new TimeSpan(00, 01, 00);
binding.OpenTimeout = new TimeSpan(00, 01, 00);
binding.ReceiveTimeout = new TimeSpan(00, 10, 00);
binding.SendTimeout = new TimeSpan(00, 10, 00);
binding.AllowCookies = false;
binding.BypassProxyOnLocal = false;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MaxBufferSize = 567890;
binding.MaxBufferPoolSize = 524288;
binding.MaxReceivedMessageSize = 567890;
binding.MessageEncoding = WSMessageEncoding.Text;
//binding.TextEncoding= "utf-8";
binding.TransferMode = TransferMode.Buffered;
binding.UseDefaultWebProxy = true;
binding.ReaderQuotas.MaxDepth=2147483647;
binding.ReaderQuotas.MaxStringContentLength=2147483647;
binding.ReaderQuotas.MaxArrayLength=2147483647;
binding.ReaderQuotas.MaxBytesPerRead=2147483647;
binding.ReaderQuotas.MaxNameTableCharCount=2147483647;
string ServiceUrl = "http://localhost:56620/StatementsManagerService.svc";
System.ServiceModel.EndpointAddress remoteAddress = new System.ServiceModel.EndpointAddress(ServiceUrl);
PopulateReqProService.StatementsManagerServiceClient Smsc = new PopulateReqProService.StatementsManagerServiceClient(binding, remoteAddress);
blnReturn = Smsc.MyMethod(MyParam);
但问题是,当我调用参数中包含大量数据的服务时,它会因协议(protocol)接收而失败,并且在 svclog 文件中我可以清楚地看到异常为:
The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.
有人可以帮忙吗?我没有得到任何线索。
最佳答案
您还需要设置您在 binding
中指定的值(在代码中的客户端 <ReaderQuotas>
上)您的web.config
部分!
binding.ReaderQuotas.MaxDepth = 2147483647;
binding.ReaderQuotas.MaxStringContentLength = 2147483647;
binding.ReaderQuotas.MaxArrayLength = 2147483647;
binding.ReaderQuotas.MaxBytesPerRead = 2147483647;
binding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
那么它应该可以工作:
关于wcf - 为什么我的 WCF 客户端的绑定(bind)设置不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7172830/