WCF-如何接受长字符串作为参数
时间:2020-03-06 14:44:47 来源:igfitidea点击:
我有一个简单的Web服务,它需要2个参数,一个是简单的xml安全令牌,另一个通常是一个长的xml字符串。它适用于短字符串,但较长的字符串会显示400错误消息。 maxMessageLength没有做任何允许更长的字符串的事情。
解决方案
我们还应该删除配额限制。
这是在具有Tcp绑定的代码中执行此操作的方法。
我添加了一些显示消除超时问题的代码,因为通常发送非常大的参数会导致超时问题。因此,请明智地使用代码...
当然,我们也可以在配置文件中设置这些参数。
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, true); // Allow big arguments on messages. Allow ~500 MB message. binding.MaxReceivedMessageSize = 500 * 1024 * 1024; // Allow unlimited time to send/receive a message. // It also prevents closing idle sessions. // From MSDN: To prevent the service from aborting idle sessions prematurely increase the Receive timeout on the service endpoint's binding.’ binding.ReceiveTimeout = TimeSpan.MaxValue; binding.SendTimeout = TimeSpan.MaxValue; XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas(); // Remove quotas limitations quotas.MaxArrayLength = int.MaxValue; quotas.MaxBytesPerRead = int.MaxValue; quotas.MaxDepth = int.MaxValue; quotas.MaxNameTableCharCount = int.MaxValue; quotas.MaxStringContentLength = int.MaxValue; binding.ReaderQuotas = quotas;
在配额的答案之后,我只是在web.config中完成了所有这些操作
<bindings> <wsHttpBinding> <binding name="WSHttpBinding_IPayroll" maxReceivedMessageSize="6553600"> <security mode="None"/> <readerQuotas maxDepth="32" maxStringContentLength="6553600" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> </binding> </wsHttpBinding> </bindings>