17

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.

bh213
  • 6,343
  • 9
  • 43
  • 52

5 Answers5

11

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;
}
daniloquio
  • 3,822
  • 2
  • 36
  • 56
7

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.

Jason Gerard
  • 166
  • 2
  • 6
  • This only explains how to initialize the Binding object starting from an abstract config definition. For the complete thing check out my answer :) – daniloquio Oct 20 '15 at 17:01
7

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

Yossi Dahan
  • 5,389
  • 2
  • 28
  • 50
6

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?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Better than nothing:) But I would like to get binding object from config file, load it by name. – bh213 Dec 10 '08 at 11:17
6

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");
Community
  • 1
  • 1
Philippe
  • 3,945
  • 3
  • 38
  • 56
  • 8
    Only if you know what kind of binding you are going to use, e.g. WSHttpBinding or NetTcpBiding. You lose the flexibility to change the kind of biding at runtime. – Anthony Oct 15 '09 at 12:51
  • 4
    But i need any binding, not only (WSHttpBinding) – Saw Dec 26 '12 at 11:12
  • For custom bindings: var binding = new System.ServiceModel.Channels.CustomBinding("BindingName"); – Sal Jan 12 '17 at 16:58