Reputation: 1187
I'm creating a WCF web service to be hosted on IIS, and accessed via internet for a Java Client.
I had to implement a Custom binding to acomplish some requirements, but I don't know how to setup the service via config to use this customized binding.
How can I do this?
If the first question is not possible, is there a way to translate this customized binding into a normal customBinding element defined in web.config
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;
}
}
So, as a summary here are my question:
Thanks.
Upvotes: 1
Views: 648
Reputation: 3570
Not sure if this is still an issue for you but perhaps the answer will help others.
To use your binding via .config, you need to create a class which extends 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();
}
}
Put your binding and BindingCollectionElement, eg 'MyBinding', in a Class Library and compile into an Assembly, eg called 'ServiceLib'.
Then add a reference to that assembly from my WCF Hosted Site - do this via right-click in VS2010 > Add Reference (then browse to 'bin' folder of the Class Library project mentioned above and select the 'ServiceLib' DLL).
Now that your WCF Website knows about the ServiceLib.dll, you can add a BindingExtension to the Service. You can do this with the WCF Config. Editor dialog or directly add the following to your 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>
You then use the binding in an endpoint of your service as such:
<services>
<service name="MyService">
<endpoint address="" binding="MyCustomBinding" name="MyCustomBindingEndpoint" contract="IMyService" />
</service>
</services>
Upvotes: 1