【问题标题】:Set another config file for the proxy为代理设置另一个配置文件
【发布时间】:2010-11-02 06:04:16
【问题描述】:

我有一个使用添加服务引用生成的 WCF 客户端,问题是该客户端的类将嵌入到 .msi(WIX 项目)及其配置文件中。从 msi 中,代理无法识别配置文件。我想在 msi 之外获取配置文件并告诉代理从那里读取它需要的内容。

有什么方法可以实现吗?告诉代理从默认配置以外的其他配置获取数据?

一些想法或一些例子会很棒。

谢谢, 阿德里安娜

【问题讨论】:

    标签: c# wcf configuration


    【解决方案1】:

    这就是我的做法: 从 Vasu 给我的链接,从示例中,我在我的项目中添加了 CustomClientChannel。 我发现了 2 个错误: - 如果代理配置没有行为 - 如果配置文件中有多个端点,每个端点都有其绑定,则无论端点如何,它始终采用第一个绑定。

    这样修复它:

    //in CreateDescription() modify
    
    if (serviceEndpoint.Binding == null)
    
                   {
    
                       serviceEndpoint.Binding = CreateBinding(selectedEndpoint.Binding, selectedEndpoint.BindingConfiguration, serviceModeGroup);
    
                   }
    
    ...
    
      if (serviceEndpoint.Behaviors.Count == 0 && !String.IsNullOrEmpty(selectedEndpoint.BehaviorConfiguration))
    
                   {
    
                       AddBehaviors(selectedEndpoint.BehaviorConfiguration, serviceEndpoint, serviceModeGroup);
    
                   }
    
      /// <summary>
    
           /// Configures the binding for the selected endpoint
    
           /// </summary>
    
           /// <param name="bindingName"></param>
    
           /// <param name="group"></param>
    
           /// <returns></returns>
    
           private Binding CreateBinding(string bindingName, string bindingConfiguration, ServiceModelSectionGroup group)
    
           {
    
               IBindingConfigurationElement be = null;
    
               BindingCollectionElement bindingElementCollection = group.Bindings[bindingName];
    
               if (bindingElementCollection.ConfiguredBindings.Count > 0)
    
               {
    
                   foreach (IBindingConfigurationElement bindingElem in bindingElementCollection.ConfiguredBindings)
    
                   {
    
                       if (string.Compare(bindingElem.Name, bindingConfiguration) == 0)
    
                       {
    
                           be = bindingElem;
    
                           break;
    
                       }
    
                   }
    
                   Binding binding = null;
    
                   if (be != null)
    
                   {
    
                       binding = GetBinding(be);
    
                       be.ApplyConfiguration(binding);
    
                   }
    
                   return binding;
    
               }
    
               return null;
    
           }
    

    【讨论】:

      【解决方案2】:

      我们通过创建自定义 ChannelFactory 并覆盖 CreateDescription 方法来完成它。然后您可以通过

      创建您的代理
      var lProxy = (IClientChannel)mYourChannelFactory.CreateChannel();
      lProxy.Open()
      

      查看http://weblogs.asp.net/cibrax/archive/2007/10/19/loading-the-wcf-configuration-from-different-files-on-the-client-side.aspx

      【讨论】:

      • 嗨 Vasu,非常感谢,把这个类放到我的项目中,它工作了......经过一些修复......将在我的帖子中解释......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多