【发布时间】:2012-12-23 16:44:13
【问题描述】:
我有一个使用代码创建的自托管 Web 服务:
protected void StartService(Type serviceType, Type implementedContract, string serviceDescription)
{
Uri addressTcp = new Uri(_baseAddressTcp + serviceDescription);
ServiceHost selfHost = new ServiceHost(serviceType, addressTcp);
Globals.Tracer.GeneralTrace.TraceEvent(TraceEventType.Information, 0, "Starting service " + addressTcp.ToString());
try
{
selfHost.AddServiceEndpoint(implementedContract, new NetTcpBinding(SecurityMode.None), "");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
selfHost.Description.Behaviors.Add(smb);
System.ServiceModel.Channels.Binding binding = MetadataExchangeBindings.CreateMexTcpBinding();
selfHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, "mex");
selfHost.Open();
ServiceInfo si = new ServiceInfo(serviceType, implementedContract, selfHost, serviceDescription);
try
{
lock (_hostedServices)
{
_hostedServices.Add(serviceType, si);
}
}
catch (ArgumentException)
{
//...
}
}
catch (CommunicationException ce)
{
//...
selfHost.Abort();
}
}
这没问题,但是当我尝试发送大块数据时,出现以下异常:
错误:格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数 @@@ 时出错。 InnerException 消息是“反序列化 @@@ 类型的对象时出错。读取 XML 数据时已超出最大字符串内容长度配额 (8192)。可以通过更改创建 XML 阅读器时使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性来增加此配额。有关详细信息,请参阅 InnerException。 at: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
解决方案似乎是将 MaxStringContentLength 属性添加到绑定。我了解如何在 Web.config (link) 中执行此操作:
...绑定名称="wsHttpBindingSettings" maxReceivedMessageSize="2147483647">
我正在寻找一种在代码中修改绑定的 maxReceivedMessageSize 的方法。对于我使用的绑定类型,这是否可能?
谢谢。
编辑: 在了解了更多信息(并在收到的回复的指导下)后,我理解了这个问题:我试图修改服务的 MEX 部分,该部分仅用于宣传它,请参阅link。 我应该修改 NetTcpBinding 的绑定(try 语句中的第一行)。 现在我的(工作)代码如下所示:
...
try
{
//add the service itself
NetTcpBinding servciceBinding = new NetTcpBinding(SecurityMode.None);
servciceBinding.ReaderQuotas.MaxStringContentLength = 256 * 1024;
servciceBinding.ReaderQuotas.MaxArrayLength = 256 * 1024;
servciceBinding.ReaderQuotas.MaxBytesPerRead = 256 * 1024;
selfHost.AddServiceEndpoint(implementedContract, servciceBinding, "");
...
【问题讨论】:
-
实际上,您应该查看绑定下的
<ReaderQuotas>子元素 - 这就是MaxStringContentLength设置所在的位置......