Search code examples
.netwcfwcf-binding

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.


Solution

  • 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");