tags:

views:

176

answers:

2

Hello,

I have following wrapper to help me manage the wcf client lifetime:

public class ServiceProxyHelper<TProxy, TChannel> : IDisposable
      where TProxy : ClientBase<TChannel>, new()
      where TChannel : class
   {
      private TProxy m_proxy;

      public TProxy Proxy
      {
         get
         {
            if (m_proxy != null)
            {
               return m_proxy;
            }
            throw new ObjectDisposedException("ServiceProxyHelper");
         }
      }

      protected ServiceProxyHelper()
      {
         m_proxy = new TProxy();
      }

      public void Dispose()
      {
          //....
      }
}

I'm using that in the following way:

   public class AccountServiceClientWrapper : ServiceProxyHelper<AccountServiceClient, IAccountService>
   {
   }

   public class Test()
   {
      using(AccountServiceClientWrapper wrapper = new AccountServiceClientWrapper())
      {
         wrapper.Proxy.Authenticate();
      }
   }

How I can modify that code to provide endpointConfigurationName for the client ?

wrapper.Proxy.Endpoint.Name = "MyCustomEndpointName";

Is not working. That endpointConfigurationName should be provider to service client constructor, but how I can do that using this wrapper ?

Regards

A: 

Perhaps you can use Activator.CreateInstance to create the proxy instance passing endpointConfigurationName as a parameter. For example,

protected ServiceProxyHelper(string endpointConfigurationName )
{
  m_proxy = (TProxy)Activator.CreateInstance(typeof(TProxy), endpointConfigurationName);
}

This will be an additional constructor in your wrapper to allow passing end point config name. Only flaw would be in case proxy type does not support such constructor, you will get an runtime exception instead of compile time error.

VinayC
Thanks for that.
Jarek Waliszko
A: 

I would go for specifying the instance of TProxy in the ServiceProxyHelper constructor.

protected ServiceProxyHelper(TProxy proxy)
{
  m_proxy = proxy;
}

Then, your proxy wrapper class would look like this:

public class AccountServiceClientWrapper : ServiceProxyHelper<AccountServiceClient, IAccountService>
{
private endpointCfgName = "{endpoint_here}";

public AccountServiceClientWrapper(): base(new AccountServiceClient(endpointCfgName))
        {
            //this.Proxy.ClientCredentials.UserName.UserName = "XYZ";
            //this.Proxy.ClientCredentials.UserName.Password = "XYZ";
        }
}

And then, the way you use it remains completely the same.

Of course, you need to remove the "new" keyword from the TProxy definition

Cristi