WCF自定义绑定——方案不能被计算,因为这个自定义绑定缺少transportindingelement

本文关键字:绑定 自定义 transportindingelement 因为 计算 方案 不能 WCF | 更新日期: 2023-09-27 18:16:51

我正在使用自定义绑定,并得到以下错误:

不能为这个绑定计算Scheme,因为这个CustomBinding缺少transportindingelement。每个绑定必须至少有一个派生自transportindingelement的绑定元素。

我的自定义绑定代码如下

public class MyCustomBinding : Binding
    {
        private HttpTransportBindingElement transport;
        private BinaryMessageEncodingBindingElement encoding;
        public MyCustomBinding()
            : base()
        {
            this.InitializeValue();
        }
        public override BindingElementCollection CreateBindingElements()
        {
            BindingElementCollection elements = new BindingElementCollection();
            elements.Add(this.encoding);
            elements.Add(this.transport);
            return elements;
        }
        public override string Scheme
        {
            get { return this.transport.Scheme; }
        }
        private void InitializeValue()
        {
            this.transport = new HttpTransportBindingElement();
            this.encoding = new BinaryMessageEncodingBindingElement();
        }
    }
    public class MyCustomBindingCollectionElement : BindingCollectionElement
    {
        // type of custom binding class
        public override Type BindingType
        {
            get { return typeof(MyCustomBinding); }
        }
       //  override ConfiguredBindings
        public override ReadOnlyCollection<IBindingConfigurationElement> ConfiguredBindings
        {
            get
            {
                return new ReadOnlyCollection<IBindingConfigurationElement>(
                new List<IBindingConfigurationElement>());
            }
        }
        // return Binding class object
        protected override Binding GetDefault()
        {
            return new MyCustomBinding();
        }
        public override bool ContainsKey(string name) {
            return true;
        }
        protected override bool TryAdd(string name, Binding binding, Configuration config)
        {
            return true;
        }
    }

网络。配置代码如下:

<extensions>
      <bindingExtensions>
        <!--<add name="ProxyElement" type="ADHA.Model.HttpTransportBindingElementProxy, ADHA"/>-->
      <add name="MyCustomBinding" type="ADHA.Model.MyCustomBindingCollectionElement,ADHA,
               Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </bindingExtensions>
    </extensions>
<service>      
    <services>
 <endpoint address="" binding="customBinding" bindingConfiguration="MyCustomBinding" contract="ADHA.IADHAService">
            </endpoint>
         <endpoint address="mex" binding="customBinding" contract="IMetadataExchange" />
      </service>      
    </services>
<bindings>
   <customBinding>
        <binding name="MyCustomBinding">
          <binaryMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                                 maxSessionSize="2048">
            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                     maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          </binaryMessageEncoding>        
          <textMessageEncoding
            messageVersion="Soap11WSAddressingAugust2004"/>
         <httpsTransport manualAddressing="false" maxBufferPoolSize="524288"
                         maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
         bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
        keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
        realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true"/>
        </binding>
      </customBinding>
    </bindings>
有人告诉我我的代码有什么问题吗?

WCF自定义绑定——方案不能被计算,因为这个自定义绑定缺少transportindingelement

请看这里的例子(http://webservices20.blogspot.de/2008/11/introducing-wcf-clearusernamebinding.html)。还有一个项目在github与良好的工作样本。您应该使用扩展描述中定义的新名称引用新类型。像这样:

<extensions>
  <bindingExtensions>
   <add name="myCustomBinding" type="ADHA.Model.MyCustomBindingCollectionElement, ADHA,
           Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </bindingExtensions>
</extensions>
<bindings>
  <myCustomBinding>
   <binding name="myCustomBindingConfig"> 
     <binaryMessageEncoding />
     <httpsTransport />
   </binding>
  </myCustomBinding>
</bindings>
<services>
  <service>
      <endpoint address="" binding="customBinding"
       bindingConfiguration="myCustomBindingConfig"
       contract="ADHA.IADHAService">
        </endpoint>
     <endpoint address="mex" binding="customBinding" contract="IMetadataExchange" />
  </service>      
</services>

为此,您必须使用配置创建一个自定义BindingElement。和一个自定义的带有Configuration.StandardBindingCollectionElement.