我正在创建一个要在IIS上托管的WCF Web服务,并通过Internet访问Java客户端。
我必须实现一个自定义绑定来完成一些要求,但我不知道如何通过config设置服务来使用这个自定义绑定。
我该怎么做?
如果第一个问题不可能,有没有办法将这个自定义绑定转换为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;
}
}
所以,这里的摘要是我的问题:
感谢。
答案 0 :(得分:0)
不确定这对您来说是否仍然是一个问题,但也许答案会对其他人有所帮助。
要通过.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中右键单击&gt;执行此操作。添加引用(然后浏览到上面提到的类库项目的'bin'文件夹并选择'ServiceLib'DLL)。
现在您的WCF网站了解ServiceLib.dll,您可以向服务添加BindingExtension。您可以使用WCF配置执行此操作。编辑器对话框或直接将以下内容添加到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>