WCF 关闭最佳实践

编程入门 行业动态 更新时间:2024-10-23 11:19:44
本文介绍了WCF 关闭最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我读到使用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 18:52:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141086.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!