views:

207

answers:

2

Hi,

I have a WCF client generated with Add Service Reference, problem is that the classes of this client will get embeded in a .msi (WIX project), together with its config file. From the msi, proxy doesn't recognize the config file. I want take config file outside of msi and tell the proxy to read what it needs from there.

Is there any way to achieve this? Tell proxy to get it's data from another config other than default one?

Some ideas or some example would be great.

Thanks, Adriana

A: 

We have done it by creating a custom ChannelFactory and overriding the CreateDescription Method. You can then create your proxy by

var lProxy = (IClientChannel)mYourChannelFactory.CreateChannel();
lProxy.Open()

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

Vasu Balakrishnan
Hi Vasu, thanks a lot, put that class in my project and it worked... after some fixes... Will explain in my post...
Adrya
A: 

This is how i did it: From the link that Vasu gave me, from the sample, i added CustomClientChannel in my project. I found 2 bugs tho : - if the proxy config doesn't have Behaviour - If in the config file are multiple endpoints each one with its binding, it takes always the first binding no matter of the endpoint.

Fixed it like this:

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

       }
Adrya