I would like to convert my current WCF HTTP / HTTPS binding settings to use binary message encoding, and I need to do this in code, not in XML configuration. AFAIK, I need to create a CustomBinding object and set the appropriate BindingElements, but I cannot figure out which elements should be used in my script.
Main parameters of my WCF configuration:
- use HTTP or HTTPS transport depending on the configuration (in app.config)
- use username message security
- todo: add binary encoding instead of default text
My current code for setting the binding (works, but without binary encoding):
var isHttps = Settings.Default.wcfServiceBaseAddress.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase);
var binding = new WSHttpBinding(isHttps ? SecurityMode.TransportWithMessageCredential : SecurityMode.Message);
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
I tried to use this code, but it doesnβt work - I donβt know how to set the message security element for username message security:
var custBinding = new CustomBinding();
custBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
if (isHttps)
{
custBinding.Elements.Add(SecurityBindingElement.CreateUserNameForSslBindingElement());
}
custBinding.Elements.Add(isHttps ?
new HttpsTransportBindingElement() :
new HttpTransportBindingElement());
Does anyone know how to set this up? I tried to find a similar problem / solution, but failed ...
source
share