【问题标题】:WCF: How to get Binding object from configurationWCF:如何从配置中获取绑定对象
【发布时间】:2010-09-26 05:10:45
【问题描述】:

我想从 web.config 或 app.config 中获取 Binding 对象。

所以,这段代码有效:

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

但我想做以下事情:

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

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

【问题讨论】:

    标签: .net wcf wcf-binding


    【解决方案1】:

    此答案满足 OP 要求,并且 100% 摘自 Pablo M. Cibraro 的这篇精彩帖子。

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

    此方法为您提供配置的绑定部分。

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

    此方法为您提供了您迫切需要的实际 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;
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这里只解释如何枚举配置的绑定。有关完整的内容,请查看我的答案。
      【解决方案3】:

      如果直到运行时才知道绑定的类型,可以使用以下方法:

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

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

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

      【讨论】:

      • 这里只解释了如何从一个抽象的配置定义开始初始化绑定对象。对于完整的事情,请查看我的答案:)
      【解决方案4】:

      您可以从 App.config/Web.config 实例化提供绑定配置名称的绑定。

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

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

      下面的例子展示了如何初始化一个新的实例 带有字符串参数的 WSHttpBinding 类。

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

      【讨论】:

      • 只有在您知道要使用哪种绑定的情况下,例如WSHttpBinding 或 NetTcpBiding。您失去了在运行时更改出价类型的灵活性。
      • 但是我需要任何绑定,不仅仅是(WSHttpBinding)
      • 对于自定义绑定:var binding = new System.ServiceModel.Channels.CustomBinding("BindingName");
      【解决方案5】:

      一个厚颜无耻的选择可能是使用默认构造函数创建一个实例,用作模板:

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

      然后把它藏起来并重新使用它。有什么帮助吗?

      【讨论】:

      • 总比没有好:) 但我想从配置文件中获取绑定对象,按名称加载。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 2016-04-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多