以编程方式创建自定义绑定

Posted

技术标签:

【中文标题】以编程方式创建自定义绑定【英文标题】:Creating Custom Binding programmatically 【发布时间】:2012-03-21 00:37:26 【问题描述】:

我们需要在我们的 WCF Web 服务中进行压缩,并且已经找到了一个好的解决方案:

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

只有一个问题。配置文件不是选项。如何在代码中创建这些自定义绑定?

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

这是我想出的:

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

但由于某种原因,CustomBinding 不接受 customMessageEncoding...

【问题讨论】:

【参考方案1】:

CustomBinding 类型的构造函数中的第一个参数仅接受 BindingElement 子类。您指定的类是BindingElementExtensionElement 子类(用于自定义配置Xml)。

与其将BindingElementExtensionElement 的子类传递给CustomBinding 构造函数,不如创建一个合适的BindingElement 子类实例(例如BinaryMessageEncodingBindingElement)并调用BindingElementExtensionElementApplyConfiguration 方法,传入BindingElement

然后将BindingElement 的实例传递给CustomBinding 构造函数。

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

【讨论】:

以上是关于以编程方式创建自定义绑定的主要内容,如果未能解决你的问题,请参考以下文章

WCF - 如何使用 HTTP(S) 上的二进制编码以编程方式创建自定义绑定

以编程方式为 VueJS 中的动态组件绑定自定义事件

以编程方式创建自定义 UICollectionFlowLayout

以编程方式填充 UITableView 以创建自定义下拉菜单

如何以编程方式创建自定义导航栏?

iOS:使用 IB 为以编程方式创建的 UITableView 创建自定义单元格