2010-01-05 8 views
8

मेरे पास एक कस्टम इंट्रानेट एप्लिकेशन है जिसमें कोई इंटरऑपरेबिलिटी आवश्यकता नहीं है। हम प्रोग्रामिंग रूप से संदेश पास करने के लिए डुप्लेक्स मोड में नेटटीसीपी चैनल का निर्माण करते हैं। हम संदेश एन्कोडिंग को बदलना चाहते थे, लेकिन यह समझने में सक्षम नहीं है कि ऐसा कैसे किया जाए। जाहिर हैनेटटीसीपी के लिए आप प्रोग्रामिंग रूप से डब्लूसीएफ संदेश एन्कोडिंग कैसे बदलते हैं?


internal class LeanTcpBinding : NetTcpBinding 
{ 
    private static readonly ILog _log = 
     LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 

    public override BindingElementCollection CreateBindingElements() 
    { 
     BindingElementCollection bindingElementCollection = base.CreateBindingElements(); 
     BindingElement encodingElement = bindingElementCollection.FirstOrDefault(
      bindingElement => bindingElement is BinaryMessageEncodingBindingElement); 
     if (encodingElement != null) 
     { 
      int index = bindingElementCollection.IndexOf(encodingElement); 
      bindingElementCollection.RemoveAt(index); 
      bindingElementCollection.Insert(index, new LeanBinaryMessageEncodingBindingElement()); 
     } 
     else 
     { 
      _log.Warn("Encoding not found"); 
     } 

     return bindingElementCollection; 
    } 
} 

, हम अपने साथ डिफ़ॉल्ट BinaryMessageEncodingBindingElement को बदलने के लिए कोशिश कर रहे हैं:

दृष्टिकोण हम ले लिया है (असफल) LeanTcpBinding नामक एक नया वर्ग में NetTcpBinding विस्तार करने के लिए इस प्रकार किया गया है। यह BinaryMessageEncodingBindingElement की जगह


internal class LeanBinaryMessageEncodingBindingElement : MessageEncodingBindingElement 
{ 
     private readonly BinaryMessageEncodingBindingElement _bindingElement; 

     /// 
     /// Initializes a new instance of the class. 
     /// 
     public LeanBinaryMessageEncodingBindingElement() 
     { 
      _bindingElement = new BinaryMessageEncodingBindingElement(); 
     } 

     /// 
     /// Initializes a new instance of the class. 
     /// 
     /// The binding element. 
     public LeanBinaryMessageEncodingBindingElement(BinaryMessageEncodingBindingElement bindingElement) 
     { 
      _bindingElement = bindingElement; 
     } 

     /// 
     /// Initializes a new instance of the class. 
     /// 
     /// The element to be cloned. 
     /// The binding element. 
     public LeanBinaryMessageEncodingBindingElement(MessageEncodingBindingElement elementToBeCloned, BinaryMessageEncodingBindingElement bindingElement) 
      : base(elementToBeCloned) 
     { 
      _bindingElement = bindingElement; 
     } 

     /// 
     /// When overridden in a derived class, returns a copy of the binding element object. 
     /// 
     /// 
     /// A object that is a deep clone of the original. 
     /// 
     public override BindingElement Clone() 
     { 
      return new LeanBinaryMessageEncodingBindingElement(
       (BinaryMessageEncodingBindingElement)_bindingElement.Clone()); 
     } 

     /// 
     /// When overridden in a derived class, creates a factory for producing message encoders. 
     /// 
     /// 
     /// The used to produce message encoders. 
     /// 
     public override MessageEncoderFactory CreateMessageEncoderFactory() 
     { 
      return new LeanBinaryMessageEncoderFactory(_bindingElement.CreateMessageEncoderFactory()); 
     } 

     /// 
     /// When overridden in a derived class, gets or sets the message version that can be handled by the message encoders produced by the message encoder factory. 
     /// 
     /// 
     /// The used by the encoders produced by the message encoder factory. 
     /// 
     public override MessageVersion MessageVersion 
     { 
      get { return _bindingElement.MessageVersion; } 
      set { _bindingElement.MessageVersion = value; } 
     } 
} 

जब मैं बाध्यकारी यह होता है मैं वास्तव में क्या लगता है कि यह करना चाहिए उपयोग करने का प्रयास ...: उपयोग सिर्फ पाने के लिए शुरू कर दिया, LeanBinaryMessageEncodingBindingElement BinaryMessageEncodingBindingElement का ही विस्तार इस प्रकार है। हालांकि, मुझे LeanBinaryMessageEncodingBindingElement.CreateMessageEncoderFactory() पर कभी भी कोई कॉल नहीं मिलती है, भले ही संदेशों को चैनल में आदान-प्रदान किया जा रहा हो।

किसी को भी यह ठीक से करने के तरीके पर कोई सुझाव है?

+0

आपने क्या हासिल करने की कोशिश की? स्थायित्व? क्या यह सुधार हुआ? –

उत्तर

7

केनी वुल्फ स्पष्ट किया क्या याद आ रही थी और इसके नीचे ब्लॉग प्रविष्टि में दर्ज है।

http://kennyw.com/?p=170

अधीर के लिए, समस्या यह है कि डिफ़ॉल्ट रूप से MessageEncoderBindingElement संदर्भ के बंधन मापदंडों के ही नहीं जोड़ता है। नतीजतन, जब परिवहन बाद में MessageEncoderBindingElement को खोजने के लिए जाता है, तो मुझे वह नहीं मिल सकता है जिसे मैंने (या आपने) बनाया है और चुप विफलता है जिसे मैंने अपनी मूल पोस्ट में नोट किया था।

दुर्भाग्य से, आपको यह सुनिश्चित करने के लिए कि आप संदर्भ के बाध्यकारी पैरामीटर में बाध्यकारी तत्व जोड़ रहे हैं, आपको सभी CanBuildXXX और BuildXXX विधियों को ओवरराइड करना होगा।

 

     public override bool CanBuildChannelFactory(BindingContext context) 
     { 
      if (context == null) { 
       throw new ArgumentNullException("context"); 
      } 

      context.BindingParameters.Add(this); 
      return context.CanBuildInnerChannelFactory(); 
     } 

     public override bool CanBuildChannelListener(BindingContext context) 
     { 
      if (context == null) { 
       throw new ArgumentNullException("context"); 
      } 

      context.BindingParameters.Add(this); 
      return context.CanBuildInnerChannelListener(); 
     } 

     public override IChannelFactory BuildChannelFactory(BindingContext context) 
     { 
      if (context == null) { 
       throw new ArgumentNullException("context"); 
      } 

      context.BindingParameters.Add(this); 
      return context.BuildInnerChannelFactory(); 
     } 

     public override IChannelListener BuildChannelListener(BindingContext context) 
     { 
      if (context == null) { 
       throw new ArgumentNullException("context"); 
      } 

      context.BindingParameters.Add(this); 
      return context.BuildInnerChannelListener(); 
     } 
 
+1

मुझे खेद है कि यह एक बेवकूफ सवाल है, लेकिन आपने यह कोड कहां रखा? ग्राहक की प्रॉक्सी में? सर्वर पक्ष के बारे में क्या? –

2

क्या आपने NetTcp बाइंडिंग कॉन्फ़िगरेशन के बजाय कस्टमबैंडिंग बनाने का प्रयास किया है? कुछ इस तरह:

NetTcpBinding binding = new NetTcpBinding(); // get binding from somewhere 
var elements = binding.CreateBindingElements(); 
BindingElementCollection newElements = new BindingElementCollection(); 
foreach (var e in elements) { 
    if (e is MessageEncodingBindingElement) { 
     newElements.Add(new MyMessageEncodingBindingElement()); 
    } else { 
     newElements.Add(e); 
    } 
} 
CustomBinding cb = new CustomBinding(newElements); 
+1

मैंने उस विधि को भी आजमाया। CreateMessageEncoderFactory() विधि को अभी भी LeanBinaryMessageEncodingBindingElement पर नहीं कहा जा रहा है - परिणाम यह है कि, मेरे एन्कोडर का निर्माण नहीं किया जा रहा है। मुझे कुछ स्पष्ट याद आना है। – Ajaxx

+0

यह .NET 4: D – markmnl

+0

@markmnl में काम करता है मेरे लिए काम नहीं करता है .NET 4.5.1 –

संबंधित मुद्दे