[英]How to use a user defined binding on an IIS hosted wcf?
我正在创建一个WCF Web服务,以托管在IIS上,并可以通过Internet访问Java客户端。
我必须实现一个自定义绑定来满足一些要求,但是我不知道如何通过配置来设置服务以使用此自定义绑定。
我怎样才能做到这一点?
如果第一个问题不可行,有没有办法将此自定义绑定转换为web.config中定义的常规customBinding元素
public class MyCustomBinding : Binding
{
public override BindingElementCollection CreateBindingElements()
{
BindingElementCollection be = new BindingElementCollection();
X509SecurityTokenParameters initiator = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.IssuerSerial, SecurityTokenInclusionMode.AlwaysToRecipient);
initiator.RequireDerivedKeys = false;
X509SecurityTokenParameters recipient = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.IssuerSerial, SecurityTokenInclusionMode.AlwaysToInitiator);
recipient.RequireDerivedKeys = false;
AsymmetricSecurityBindingElement element = new AsymmetricSecurityBindingElement(recipient, initiator);
element.SetKeyDerivation(false);
element.IncludeTimestamp = true;
element.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
element.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
element.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10
element.DefaultAlgorithmSuite = SecurityAlgorithmSuite.TripleDesRsa15;
element.AllowSerializedSigningTokenOnReply = true;
X509SecurityTokenParameters tokenParameters = new X509SecurityTokenParameters();
tokenParameters.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient;
tokenParameters.RequireDerivedKeys = false;
element.EndpointSupportingTokenParameters.Signed.Add(tokenParameters);
be.Add(element);
be.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressing10, Encoding.UTF8));
be.Add(new HttpTransportBindingElement());
return be;
}
}
因此,作为总结,这是我的问题:
谢谢。
不知道这是否仍然是您的问题,但答案可能会帮助其他人。
要通过.config使用绑定,您需要创建一个扩展BindingCollectionElement的类。
public class MyCustomBindingCollectionElement : BindingCollectionElement { public override Type BindingType { get { return typeof(MyCustomBinding); } } public override ReadOnlyCollection<IBindingConfigurationElement> ConfiguredBindings { get { return new ReadOnlyCollection<IBindingConfigurationElement>( new List<IBindingConfigurationElement>()); } } public override bool ContainsKey(string name) { // HACK!!! return true; //throw new NotImplementedException(); } protected override System.ServiceModel.Channels.Binding GetDefault() { return new MyCustomBinding(); } protected override bool TryAdd(string name, System.ServiceModel.Channels.Binding binding, Configuration config) { throw new NotImplementedException(); } }
将您的绑定和BindingCollectionElement(例如“ MyBinding”)放在类库中,然后编译为一个程序集(例如,称为“ ServiceLib”)。
然后从我的WCF托管站点添加对该程序集的引用-通过右键单击VS2010>添加引用(然后浏览到上述类库项目的“ bin”文件夹并选择“ ServiceLib” DLL)来执行此操作。
现在,您的WCF网站已经了解ServiceLib.dll,可以将BindingExtension添加到服务中了。 您可以使用WCF Config进行此操作。 编辑器对话框或将以下内容直接添加到您的web.config中:
<system.serviceModel>
...
<extensions>
<bindingExtensions>
<add name="MyCustomBinding" type="MyCustomBindingCollectionElement, ServiceLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</bindingExtensions>
</extensions>
...
</system.serviceModel>
然后,您可以在服务的端点中按如下方式使用绑定:
<services>
<service name="MyService">
<endpoint address="" binding="MyCustomBinding" name="MyCustomBindingEndpoint" contract="IMyService" />
</service>
</services>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.