简体   繁体   中英

WCF client proxy for ASP .NET client

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM