tags:

views:

63

answers:

1

I need some advice on what's the best way to create WCF client proxy wrapper for ASP .NET client. I want to work equally well with ObjectDataSource control with no extra coding and also when I need to manually call WCF service to get some data. I basically have come up with two models, but I'd like to know which is is more efficient.

Here is the first client wrapper

public class Facade1 : IDisposable
{
   private readonly IClient proxy = ClientProxyFactory.GetObject<IClient>();

   public List<string> GetData()
   {
      proxy.GetData()
   }

   public List<string> GetMoreData()
   {
      proxy.GetMoreData()
   }

   public void Dispose()
   {
       ClientProxyFactory.CloseChannel(this.proxy);
   }
}

Now here is another WCF wrapper.

public class Facade2
{
   public List<string> GetData()
   {
        IClient proxy = ClientProxyFactory.GetObject<IClient>();

        try
        {
            return client.GetData();
        }
        finally
        {
            ClientProxyFactory.CloseChannel(proxy);            
        }
   }

   public List<string> GetMoreData()
   {
        IClient proxy = ClientProxyFactory.GetObject<IClient>();

        try
        {
            return client.GetMoreData();
        }
        finally
        {
            ClientProxyFactory.CloseChannel(proxy);            
        }
   }
}

In the first example, there is only one instance of the client proxy and it can be reused between various methods, but the class needs to implement IDisposable so that the proxy can be correctly disposed by the client. In the second example, there is one client proxy per method and the client does not have worry about disposing the proxy.

Is reusing proxy between different method a good way to go? Is there performance hit when you open/close WCF proxy? (In both examples, assume that ChannelFactory is cached and new channel is created every time via cached_factory.CreateChannel() method.)

For example, with the first wrapper I can do something like:

using (Facade1 facade = new Facade1())
{
   facade.GetData()
   ...
   ...
   facade.GetMoreData()
}

In the second example, I can just instantiate my facade and call the needed methods without worrying about disposing a proxy.

Thanks in advance,

Eric

+1  A: 

If you use this wrapper for multiple calls to WCF service in single HTTP request processing in your ASP.NET application than the model with shared proxy is better. If you want to share the wrapper (make it global) then second model should be used.

Performance of recreating a proxy is dependent on type of used binding and its configuration. For example in case of BasicHttpBinding recreation of a proxy can be quick because there can still exists persistant HTTP connection from previous proxy. But in case of WSHttpBinding with security context, recreation of proxy means new security handshake for estabilishing security session.

Ladislav Mrnka
I was going to create a separate assembly for first wrapper or second wrapper, depending on which one I choose. Is there an issue with sharing a wrapper in the first model? Now for performance issues, right now the service is hosted in Windows 2003 server w/IIS6, so I just use basicHttpBinding, but in the future, we may upgrade to Windows Server 2008 and host it in IIS7 where I can use netTcpBinding with WAS in IIS7. I kind of like the first model where proxy is shared between methods. The only downside is that the client has to remember to dispose wrapper correctly.
Eric