2009-06-09 96 views
17

我有一个服务的动态客户端。我如何更改它的端点绑定的ReaderQuotas属性?以编程方式修改端点ReaderQuotas

我想这样的,但它不工作...

DynamicProxyFactory factory = new DynamicProxyFactory(m_serviceWsdlUri); 

foreach (ServiceEndpoint endpoint in factory.Endpoints) 
{ 
    Binding binding = endpoint.Binding; 

    binding.GetProperty<XmlDictionaryReaderQuotas>(new BindingParameterCollection()).MaxArrayLength = 2147483647 
    binding.GetProperty<XmlDictionaryReaderQuotas>(new BindingParameterCollection()).MaxBytesPerRead =2147483647; 
    binding.GetProperty<XmlDictionaryReaderQuotas>(new BindingParameterCollection()).MaxDepth = 2147483647; 
    binding.GetProperty<XmlDictionaryReaderQuotas>(new BindingParameterCollection()).MaxNameTableCharCount = 2147483647; 
    binding.GetProperty<XmlDictionaryReaderQuotas>(new BindingParameterCollection()).MaxStringContentLength = 2147483647; 
    } 

即使这样做值保持在默认的的ReaderQuotas后。

我也试过这样,仍然不起作用:

 DynamicProxyFactory factory = new DynamicProxyFactory(m_serviceWsdlUri); 

    foreach (ServiceEndpoint endpoint in factory.Endpoints) 
    { 
     System.ServiceModel.Channels.BindingElementCollection bec = endpoint.Binding.CreateBindingElements(); 

     System.ServiceModel.Channels.TransportBindingElement tbe = bec.Find<System.ServiceModel.Channels.TransportBindingElement>(); 

     tbe.MaxReceivedMessageSize = 2147483647; 
     tbe.MaxBufferPoolSize = 2147483647; 
     TextMessageEncodingBindingElement textBE = bec.Find<TextMessageEncodingBindingElement>(); 

     if (textBE != null) 
     { 

      textBE.ReaderQuotas.MaxStringContentLength = 2147483647; 
      textBE.ReaderQuotas.MaxArrayLength = 2147483647; 
      textBE.ReaderQuotas.MaxBytesPerRead = 2147483647; 
      textBE.ReaderQuotas.MaxDepth = 2147483647; 
      textBE.ReaderQuotas.MaxNameTableCharCount = 2147483647; 

     } 
    } 

我需要这个,所以我可以超过8KB到服务发送更多。

回答

33

创建绑定后在BindingElement上设置配额对该绑定没有影响。

编辑(因为你不知道是做什么用绑定):

您可以使用反射来设置该属性。请注意,在设置之前,应确保绑定实际上具有属性。并非所有的绑定都有这个属性。如果您尝试将其设置为不支持它的绑定,则该示例将引发异常。

Binding binding = endpoint.Binding; 

XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas(); 
myReaderQuotas.MaxStringContentLength = _sanebutusablelimit_; 
myReaderQuotas.MaxArrayLength = _sanebutusablelimit_; 
myReaderQuotas.MaxBytesPerRead = _sanebutusablelimit_; 
myReaderQuotas.MaxDepth = _sanebutusablelimit_; 
myReaderQuotas.MaxNameTableCharCount = _sanebutusablelimit_; 

binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, myReaderQuotas, null); 

希望这可以帮助你一点。

+4

+1提这些事以前的客户端代理和/或服务主机创建进行设置。一旦创建,它们是不可变的。 – 2009-06-09 12:02:03

+0

嗨马克, 感谢您的答复,但我不知道它是什么样的绑定,这就是为什么我需要做绑定后创建。 还有其他建议吗? 谢谢,阿德里亚 – Adrya 2009-06-09 12:08:38

9

你为什么要解决,在一个如此复杂的方式,只是改变直接ReaderQuotas:

即:

WsHttpBinding的WebBinding =新WsHttpBinding的();

WebBinding.ReaderQuotas.MaxArrayLength = int.MaxValue; WebBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue; WebBinding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;

这将工作没有那个怪异的反射样本。

干杯

0

另外需要指出的是,绑定的以下特性还需要完整的解决方案进行更新:

binding2.MaxBufferSize = 2147483647; 
binding2.MaxReceivedMessageSize = 2147483647; 

对于其他人在这里的好处是样品以编程方式将客户端,并与2个属性以上沿着服务器上的ReaderQuotas:

客户机代码:

 WebHttpBinding binding2 = new WebHttpBinding(); 
     XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas(); 
     myReaderQuotas.MaxStringContentLength = 2147483647; 
     myReaderQuotas.MaxArrayLength = 2147483647; 
     myReaderQuotas.MaxBytesPerRead = 2147483647; 
     myReaderQuotas.MaxDepth = 2147483647; 
     myReaderQuotas.MaxNameTableCharCount = 2147483647; 

     binding2.GetType().GetProperty("ReaderQuotas").SetValue(binding2, myReaderQuotas, null); 
     binding2.MaxBufferSize = 2147483647; 
     binding2.MaxReceivedMessageSize = 2147483647; 
     ServiceEndpoint ep = new ServiceEndpoint(ContractDescription.GetContract(typeof(IMyService)), 
      binding2, new EndpointAddress("http://localhost:9000/MyService")); 

     WebChannelFactory<IMyService> cf2 = new WebChannelFactory<IMyService>(ep); 

     IMyService serv = cf2.CreateChannel(); 
     serv.PrintNameDesc("Ram", new string('a', 100*1024*1024)); 

Server代码:

 WebHttpBinding binding2 = new WebHttpBinding(); 
     XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas(); 
     myReaderQuotas.MaxStringContentLength = 2147483647; 
     myReaderQuotas.MaxArrayLength = 2147483647; 
     myReaderQuotas.MaxBytesPerRead = 2147483647; 
     myReaderQuotas.MaxDepth = 2147483647; 
     myReaderQuotas.MaxNameTableCharCount = 2147483647; 

     binding2.GetType().GetProperty("ReaderQuotas").SetValue(binding2, myReaderQuotas, null); 
     binding2.MaxBufferSize = 2147483647; 
     binding2.MaxReceivedMessageSize = 2147483647; 

     WebServiceHost host2 = new WebServiceHost(typeof(MyService)); 
     host2.AddServiceEndpoint(typeof(IMyService), binding2, new Uri("http://localhost:9000/MyService")); 

     host2.Open(); 

如果合同是:

[ServiceContract] 
public interface IMyService 
{ 
    [WebInvoke(Method = "PUT", 
     UriTemplate = "My/{name}/", 
     BodyStyle = WebMessageBodyStyle.Bare, 
     ResponseFormat = WebMessageFormat.Xml, 
     RequestFormat = WebMessageFormat.Xml)] 
    [OperationContract] 
    void PrintNameDesc(string name, string desc); 
} 
相关问题