Search code examples
c#wcfwcf-bindingwcf-configuration

How to set the WCF binding scheme via code?


I need to reproduce this app.config service file via C# code.

<system.serviceModel>
....
  <protocolMapping>
     <add scheme="https" binding="wsHttpBinding" />
  </protocolMapping>
...

Specially the scheme part, so i did this:

   var binding = new WSHttpBinding();
   binding.Security.Mode = SecurityMode.Transport;
   ...

But if i try to do a binding.Scheme = ... i get the compile time error the property Scheme has no setter.

How to set it via code?


Solution

  • The binding.Scheme is set automatically by the class when you set binding.Security.Mode. For instance, if you set binding.Security.Mode = SecurityMode.None;, then it returns "http" and if you set binding.Security.Mode = SecurityMode.Transport; then it returns "https".

    WSHttpBinding reference: http://msdn.microsoft.com/en-us/library/system.servicemodel.wshttpbinding%28v=vs.110%29.aspx

    Also, if you google setting up a wcf service in code there seems to be lots of interesting examples to help you on your way.