.net WCF:如何从配置中获取绑定对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/355576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:46:54  来源:igfitidea点击:

WCF: How to get Binding object from configuration

.netwcfwcf-binding

提问by bh213

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

我想从 web.config 或 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.

我当然对 DoSomething() 方法感兴趣。

采纳答案by Philippe

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

您可以从 App.config/Web.config 实例化一个绑定,给出一个绑定配置名称。

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

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

使用由其配置名称指定的绑定初始化 WSHttpBinding 类的新实例。

以下示例显示如何使用字符串参数初始化 WSHttpBinding 类的新实例。

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

回答by daniloquio

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

这个答案满足了 OP 的要求,并且 100% 是从 Pablo M. Cibraro 的这篇精彩文章中提取的。

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

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 Bindingobject you are so desperately needing.

这种方法为您提供了您Binding非常需要的实际对象。

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

回答by Yossi Dahan

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

查看Mark Gabarra 的这篇博文,它展示了如何枚举配置的绑定

回答by Jason Gerard

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.

其中绑定类型的bindingType 和endpointConfigName 是配置文件中指定的名称。

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.

所有包含的绑定都提供了一个构造函数,该构造函数将 endpointConfigurationName 作为唯一参数,因此这应该适用于所有绑定。我已经将它用于 WsHttpBinding 和 NetTcpBinding 没有问题。

回答by Marc Gravell

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?

然后将其收起并重新使用。有什么帮助吗?