Creating Custom Binding programmatically

那年仲夏 提交于 2020-01-15 10:12:30

问题


We need compression in our WCF Web Service and have already found a good solution:

http://www.codeproject.com/Articles/53718/Extending-WCF-Part-II

There's only one problem. Configuration files are no option. How those Custom Bindings can be created in code?

  <customBinding>
    <binding name="ZipBinding" closeTimeout="00:10:00" 
openTimeout="00:10:00" receiveTimeout="00:10:00" 
    sendTimeout="00:10:00">
      <customMessageEncoding innerMessageEncoding="mtomMessageEncoding" 
    messageEncoderType="YourAssemblyName.YourMessageEncoder, 
    WcfExtensions">
        <readerQuotas maxDepth="999999999" 
            maxStringContentLength="999999999" 
    maxArrayLength="999999999" maxBytesPerRead="999999999" 
    maxNameTableCharCount="999999999">
        </readerQuotas>
      </customMessageEncoding>
      <httpTransport maxBufferSize="999999999" 
        maxReceivedMessageSize="999999999" 
    authenticationScheme="Anonymous" 
    proxyAuthenticationScheme="Anonymous" useDefaultWebProxy="true"/>
    </binding>
  </customBinding>

This is what i've come up with:

MessageEncodingBindingElementExtension customMessageEncoding = new MessageEncodingBindingElementExtension
{
    InnerMessageEncoding = "binaryMessageEncoding",
    MessageEncoderType = "WcfExtensions.GZipMessageEncoder, WcfExtensions",
};
customMessageEncoding.ReaderQuotas.MaxDepth = 999999999;
customMessageEncoding.ReaderQuotas.MaxStringContentLength = 999999999;
customMessageEncoding.ReaderQuotas.MaxArrayLength = 999999999;
customMessageEncoding.ReaderQuotas.MaxBytesPerRead = 999999999;
customMessageEncoding.ReaderQuotas.MaxNameTableCharCount = 999999999;

CustomBinding zipBinding = new CustomBinding(customMessageEncoding, new HttpTransportBindingElement
{
    MaxBufferSize = 999999999,
    MaxReceivedMessageSize = 999999999,
    AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous,
    ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous,
    UseDefaultWebProxy = true
});

zipBinding.CloseTimeout = new TimeSpan(0, 10, 0);
zipBinding.OpenTimeout = new TimeSpan(0, 10, 0);
zipBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
zipBinding.SendTimeout = new TimeSpan(0, 10, 0);

But for some reason CustomBinding doesn't accept customMessageEncoding...


回答1:


The first parameter in the constructor for the type CustomBinding accepts only a BindingElement subclass. The class you have specified is a BindingElementExtensionElement subclass (used for customising the configuration Xml).

Instead of passing a subclass of the BindingElementExtensionElement into the CustomBinding constructor, you should create an suitable subclass instance of a BindingElement(e.g. BinaryMessageEncodingBindingElement) and call the BindingElementExtensionElement’s ApplyConfiguration method, passing in the BindingElement.

Then pass the instance of the BindingElement into the CustomBinding constructor.

BinaryMessageEncodingBindingElement encodingElement = new BinaryMessageEncodingBindingElement();
customMessageEncoding.ApplyConfiguration(encodingElement);

CustomBinding zipBinding = new CustomBinding(encodingElement, new HttpTransportBindingElement
{
    MaxBufferSize = 999999999,
    MaxReceivedMessageSize = 999999999,
    AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous,
    ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous,
    UseDefaultWebProxy = true
});


来源:https://stackoverflow.com/questions/9534478/creating-custom-binding-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!