WCF 关闭最佳实践 [英] WCF closing best practice

查看:27
本文介绍了WCF 关闭最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到使用 WCF 代理的最佳做法是:

I read that the best practice for using WCF proxy would be:

YourClientProxy clientProxy = new YourClientProxy();

try
{
   .. use your service
   clientProxy.Close();
}
catch(FaultException)
{
   clientProxy.Abort();
}
catch(CommunicationException)
{
   clientProxy.Abort();
}
catch (TimeoutException)
{ 
   clientProxy.Abort();
}

我的问题是,在分配代理后,我为其分配事件处理程序,并使用代理初始化其他方法:

My problem is, after I allocate my proxy, I assign event handlers to it and also initialize other method using the proxy:

public void InitProxy()
{
    sdksvc = new SdkServiceClient();
    sdksvc.InitClusteringObjectCompleted += new EventHandler<InitClusteringObjectCompletedEventArgs>(sdksvc_InitClusteringObjectCompleted);
    sdksvc.InitClusteringObjectAsync(Utils.DSN, Utils.USER,Utils.PASSWORD);
    sdksvc.DoClusteringCompleted += new EventHandler<DoClusteringCompletedEventArgs>(sdksvc_DoClusteringCompleted);
    sdksvc.CreateTablesCompleted += new EventHandler<CreateTablesCompletedEventArgs>(sdksvc_CreateTablesCompleted);
}

如果我想按照最佳实践建议使用代理,我现在需要在每次使用代理时调用 InitProxy() 方法.

I now need to call the InitProxy() method each Time I use the proxy if I want to use it as best practice suggests.

关于如何避免这种情况的任何想法?

Any ideas on how to avoid this?

推荐答案

有几个选项.一种选择是编写一个帮助类,如下所示:

There are several options. One option is to write a helper class as follows:

public class SvcClient : IDisposable {
   public SvcClient(ICommunicationObject service) {
      if( service == null ) {
         throw ArgumentNullException("service");
      }
      _service = service;
      // Add your event handlers here, e.g. using your example:
      sdksvc = new SdkServiceClient();
      sdksvc.InitClusteringObjectCompleted += new EventHandler<InitClusteringObjectCompletedEventArgs>(sdksvc_InitClusteringObjectCompleted);
      sdksvc.InitClusteringObjectAsync(Utils.DSN, Utils.USER,Utils.PASSWORD);
      sdksvc.DoClusteringCompleted += new EventHandler<DoClusteringCompletedEventArgs>(sdksvc_DoClusteringCompleted);
      sdksvc.CreateTablesCompleted += new EventHandler<CreateTablesCompletedEventArgs>(sdksvc_CreateTablesCompleted);
   }
   public void Dispose() {
      try {
         if( _service.State == CommunicationState.Faulted ) {
            _service.Abort();
         }
      }
      finally {
         _service.Close();
      }
   }
   private readonly ICommunicationObject _service;
}

要使用此类,请编写以下内容:

To use this class write the following:

var clientProxy = new YourClientProxy();
using(new SvcClient(clientProxy)) {
   // use clientProxy as usual. No need to call Abort() and/or Close() here.
}

SvcClient 的构造函数被调用时,它会根据需要设置SdkServiceClient 实例.此外,SvcClient 类会根据需要清理服务客户端代理以及中止和/或关闭连接,而不管控制流如何离开 using 块.

When the constructor for SvcClient is called it then sets up the SdkServiceClient instance as desired. Furthermore the SvcClient class cleans up the service client proxy as well aborting and/or closing the connection as needed regardless of how the control flow leaves the using-block.

这篇关于WCF 关闭最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆