سؤال

لدي عميل ديناميكي إلى خدمة. كيف يمكنني تغيير خاصية Readerquotas Binding هي نقطة النهاية؟

حاولت هذا ولكن هذا لا يعمل ...

 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;

         }
   }

أحتاج إلى هذا حتى أتمكن من إرسال أكثر من 8 كيلو بايت إلى الخدمة.

هل كانت مفيدة؟

المحلول

وضع الحصص على 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);

نأمل أن يكون هذا يساعدك قليلا.

نصائح أخرى

لماذا تحل ذلك بطريقة معقدة مثل هذه، فقط تغيير Readerquotas مباشرة:

بمعنى آخر:

Wshttpginding Webbinding = جديد wshttpginding ()؛

WebBinding.Readerquotas.maxarraylength = int.maxvalue؛ webbinding.readerquotas.maxstringcontentlength = int.maxvalue؛ webbinding.readerquotas.maxbytesperread = int.maxvalue؛

هذا سيعمل بدون عينة الانعكاس الغريب.

هتافات

أيضا أن يلاحظ، هو أن الخصائص التالية للربط تحتاج أيضا إلى تحديث الحل الكامل:

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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top