0

Is there any generic way to add transport security to client endpoint binding dynamically for WCF service?

The binding can be accessed using proxyObject.Endpoint.Binding property. The Security property for each binding is specific to each binding.

Edit:

If binding cannot be updated after proxy is created, is there any generic way to configure proxy binding with transport security. I am using below overload to create proxy. The binding needs to be created based on client endpoint and binding configuration defined in configuration file.

protected ClientBase(Binding binding, EndpointAddress remoteAddress);

My Code:

Binding binding;

// Code to create binding using details from configuration file goes here...  

// For example, if configuration uses BasicHttpBinding,  
// then here we will create object of "BasicHttpBinding" and
// set its Security mode as Transport

TServiceProxyInterface serviceProxy = (TServiceProxyInterface)Activator.CreateInstance(
                                         TServiceProxyClass,
                                         binding,
                                         new EndpointAddress(serviceAddress));
Sambhaji
  • 990
  • 5
  • 19
  • 31
  • You can't change the properties and settings on a proxy once you've created it. You'd need to create a new proxy with the new settings. – Tim Nov 11 '15 at 21:21
  • Thanks Tim! I have updated my question. – Sambhaji Nov 11 '15 at 22:35
  • With the overload you're using, you can programatically declare the `binding` object and pass that in. Can you post more of the code you're planning to use? I have at least one (maybe more) approaches you can use, but it partly depends on what you're doing. Note that the overload you're planning to use has nothing to do with what is in the configuration file. – Tim Nov 11 '15 at 22:42
  • I have added my intended code. Although, we can pass any binding object to the overload that I am using, I want to create binding using the data from configuration file since my entire logic for creating proxy is generic. – Sambhaji Nov 12 '15 at 00:34
  • One last question - when you say your using data from the configuration file, are using items from `` or items from another section, like ``? – Tim Nov 12 '15 at 00:51
  • The configuartion details from . – Sambhaji Nov 12 '15 at 17:52

1 Answers1

1

If I understood your question correctly, you want to load the configuration data from the config file and then set the security. I think you can do something like this:

BasicHttpBinding binding = new BasicHttpBinding("BindingConfigurationName");
binding.Security.Mode = BasicHttpSecurity.Transport;
TServiceProxyInterface serviceProxy = (TServiceProxyInterface)Activator.CreateInstance(
                                     TServiceProxyClass,
                                     binding,
                                     new EndpointAddress(serviceAddress));

This example uses the overload of the BasicHttpSecurity that takes the binding configuration name from the config file. You should be able to set the security on it since you haven't created the proxy yet.

It gets a little trickier if you have different binding protocols. At work our core framework uses a client definition in a custom configuration section that specifies the type of binding as well as the binding configuration name (among other things), and then we use ChannelFactory<T> to create a factory (or get it from cache if already created) and generate channels as needed. A switch statement is used to select the type of binding to create.

Hopefully this gets you in the right direction. Note that the code above is not tested.

Tim
  • 28,212
  • 8
  • 63
  • 76
  • Thanks Tim! Currently, I am using same code that you have given. I wanted to write generic code so that it can work with multiple protocols and I should not need to use switch case to create binding for each protocol and then set security mode. It looks like, we can use (Binding)Activator.CreateInstance(bindingType, endpointConfigName); to create binding but there is no generic way to set Security mode since it is not defined in Binding class which is a base class for all bindings. – Sambhaji Nov 12 '15 at 20:00