bh213
bh213

Reputation: 6529

WCF: How to get Binding object from configuration

I would like to get Binding object from web.config or app.config.

So, this code works:

wcfTestClient = new TestServiceClient("my_endpoint", Url + "/TestService.svc");

but I would like to do the following:

Binding binding = DoSomething();
wcfTestClient = new TestServiceClient(binding, Url + "/TestService.svc");

I am interested in DoSomething() method, of course.

Upvotes: 17

Views: 25080

Answers (5)

Philippe
Philippe

Reputation: 4051

You can instantiate a binding giving a binding configuration name from App.config/Web.config.

http://msdn.microsoft.com/en-us/library/ms575163.aspx

Initializes a new instance of the WSHttpBinding class with a binding specified by its configuration name.

The following example shows how to initialize a new instance of the WSHttpBinding class with a string argument.

// Set the IssuerBinding to a WSHttpBinding loaded from config
b.Security.Message.IssuerBinding = new WSHttpBinding("Issuer");

Upvotes: 6

daniloquio
daniloquio

Reputation: 3902

This answer fulfills the OP request and is 100% extracted from this amazing post from Pablo M. Cibraro.

http://weblogs.asp.net/cibrax/getting-wcf-bindings-and-behaviors-from-any-config-source

This method gives you the config's binding section.

private BindingsSection GetBindingsSection(string path)
{
  System.Configuration.Configuration config = 
  System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(
    new System.Configuration.ExeConfigurationFileMap() { ExeConfigFilename = path },
      System.Configuration.ConfigurationUserLevel.None);

  var serviceModel = ServiceModelSectionGroup.GetSectionGroup(config);
  return serviceModel.Bindings;
}

This method gives you the actual Binding object you are so desperately needing.

public Binding ResolveBinding(string name)
{
  BindingsSection section = GetBindingsSection(path);

  foreach (var bindingCollection in section.BindingCollections)
  {
    if (bindingCollection.ConfiguredBindings.Count > 0 
        && bindingCollection.ConfiguredBindings[0].Name == name)
    {
      var bindingElement = bindingCollection.ConfiguredBindings[0];
      var binding = (Binding)Activator.CreateInstance(bindingCollection.BindingType);
      binding.Name = bindingElement.Name;
      bindingElement.ApplyConfiguration(binding);

      return binding;
    }
  }

  return null;
}

Upvotes: 12

Jason Gerard
Jason Gerard

Reputation: 166

If you don't know the type of the binding until runtime, you can use the following:

return (Binding)Activator.CreateInstance(bindingType, endpointConfigName);

Where bindingType of the type of the binding and endpointConfigName is the name of specified in the config file.

All the included bindings provide a constructor that takes the endpointConfigurationName as the only parameter so this should work for all of them. I have used it for WsHttpBinding and NetTcpBinding without problems.

Upvotes: 7

Yossi Dahan
Yossi Dahan

Reputation: 5357

Check out this blog post from Mark Gabarra, it shows how to enumerate the configured bindings

Upvotes: 7

Marc Gravell
Marc Gravell

Reputation: 1062770

One cheeky option might be to create an instance with the default constructor, to use as a template:

Binding defaultBinding;
using(TestServiceClient client = new TestServiceClient()) {
    defaultBinding = client.Endpoint.Binding;
}

Then tuck this away and re-use it. Any help?

Upvotes: 6

Related Questions