【问题标题】:Modify endpoint ReaderQuotas programmatically以编程方式修改端点 ReaderQuotas
【发布时间】:2009-06-09 11:25:24
【问题描述】:

我有一个服务的动态客户端。如何更改其端点绑定的 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 的数据。

【问题讨论】:

    标签: c# wcf readerquotas


    【解决方案1】:

    在创建绑定后对 BindingElement 设置配额不会影响该绑定。

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

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

    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);
    

    希望对你有所帮助。

    【讨论】:

    • +1 用于提及这些内容必须在创建客户端代理和/或服务主机之前设置。一旦创建,它们就是不可变的。
    • 嗨,Marc,感谢您的回复,但我不知道它是什么类型的绑定,这就是为什么我需要在创建绑定后执行此操作。还有其他建议吗?谢谢,阿德里亚
    • 什么意思,不知道是什么绑定。在 ServiceHostFactory 中,只需查看绑定并根据需要修改配额。如果你说的是,你不知道你需要修改配额,直到你使用绑定之后,然后..可能设置一个标志,然后重新启动主机(或客户端代理)。
    • 我编辑了回复以使用反射处理未知绑定。
    • 非常感谢,正是我需要的,效果很好:)))))
    【解决方案2】:

    你为什么要用这么复杂的方式解决这个问题,直接改变 ReaderQuotas:

    即:

    WSHttpBinding WebBinding = new WSHttpBinding();

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

    如果没有那个奇怪的反射样本,这将起作用。

    干杯

    【讨论】:

    • 因为客户端在我手上时是动态创建的,所以绑定和所有内容都已经创建,我只是想增加该客户端的消息大小。
    【解决方案3】:

    另外需要注意的是,完整的解决方案也需要更新Binding的以下属性:

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

    为了其他人的利益,这里有一个示例,它以编程方式在客户端和服务器上设置 ReaderQuotas 以及上面的 2 个属性:

    客户端代码:

            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));
    

    服务器代码:

            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);
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 1970-01-01
      • 2013-12-04
      • 2015-02-27
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多