WCF: How to get Binding object from configuration

余生长醉 提交于 2020-12-01 09:13:34

问题


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.


回答1:


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



回答2:


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;
}



回答3:


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




回答4:


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.




回答5:


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?



来源:https://stackoverflow.com/questions/355576/wcf-how-to-get-binding-object-from-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!