【发布时间】:2016-01-07 07:48:14
【问题描述】:
我正在使用自定义绑定并收到以下错误:
无法为此绑定计算方案,因为此 CustomBinding 缺少 TransportBindingElement。每个绑定必须至少有一个从 TransportBindingElement 派生的绑定元素。
我的自定义绑定代码如下
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;
}
}
Web.config代码如下:
<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>
有人请告诉我我的代码有什么问题吗?
【问题讨论】:
-
我猜你已经以 (codeproject.com/Tips/243357/WCF-Custom-Binding) 为例。您是否尝试先运行您的服务而不在配置中绑定自定义?尝试删除
<binding name="MyCustomBinding">并使用默认传输和消息设置运行。如果没问题,xml中绑定自定义的问题。我猜你绑定<binding name="MyCustomBinding">不会自动匹配自定义类型中MyCustomBinding类型的绑定。应该有办法说与配置名称 A 的绑定是 .NET 类型 B 的实例 -
@Mimas 服务可以正常使用 wsHTTP 绑定,但不能使用自定义绑定实际上我必须这样做develop.com/building-scalable-and-secure-wcf-services 这就是我使用自定义绑定的原因
-
对您的自定义绑定没有任何问题。正如我所见,问题在于您如何尝试将其应用于您的服务(应用程序无法识别您在配置文件中的新绑定)。如果你想通过配置文件做到这一点,你必须实现使其工作所需的所有东西:自定义 BindingElement、自定义 BindingCollectionElement 等并进行适当的配置(我给了你一个例子)。从我的角度来看,最简单的方法是获取一个示例项目并根据您的需要进行更改。项目链接见我下面的回答。