2009-06-11 40 views
0

我有一個使用Add Service Reference生成的WCF客戶端,問題是此客戶端的類將與其配置文件一起嵌入到.msi(WIX項目)中。從msi中,代理無法識別配置文件。我想在msi以外的配置文件,並告訴代理從那裏讀取它的需求。爲代理設置另一個配置文件

有什麼辦法可以達到這個目的嗎?告訴代理從另一個配置獲取它的數據而不是默認的數據?

一些想法或一些例子會很好。

感謝, 阿德里安娜

回答

0

這是我做的: 從這個瓦蘇給我,從樣品,我在我的項目加入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; 

     } 
0

我們通過創建自定義的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

+0

瓦蘇你好,非常感謝,把這個類在我的項目和它的工作......一些修正後......將我的帖子解釋... – Adrya 2009-06-12 13:48:40

相關問題