2009-06-09 93 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); 
} 
相關問題