一:文起缘由

写这一篇的目的源自于最近看同事在写wcf的时候,用特别感觉繁琐而且云里雾里的嵌套try catch来防止client抛出异常,特别感觉奇怪,就比如下面的代码。

         public void StartNormalMarketing(int shopId, List<int> marketingIdList)
{ using (SendEventMarketingService.DistributeServiceClient client = new SendEventMarketingService.DistributeServiceClient())
{
try
{ client.StartByMarketingIDList(shopId, marketingIdList, SendEventMarketingService.MarketingType.NormalMarketing); }
catch (Exception ex)
{
LogHelper.WriteLog("常规营销活动开启服务", ex);
}
finally
{
try
{
client.Close();
}
catch (Exception)
{
client.Abort();
}
}
}
}

看完上面的代码,不知道你是否有什么感想?而且我还问了同事,为什么try catch要写成这样,同事说是根据什么书上来的什么最佳实践,这话一说,我也不敢轻易

怀疑了,只能翻翻源代码看看这话是否有道理,首先我来说说对这段代码的第一感觉。。。

1. 代码特别繁琐

  我们写代码,特别不喜欢繁琐,上面的代码就是一例,你try catch就try catch,还在finally中嵌套一个try catch,真的有点感觉像吃了两只癞蛤蟆一样。。。

2. 混淆close和abort的用法  

  这种代码给人的感觉就是为什么不精简一下呢???比如下面这样,起码还可以少写一对try catch,对吧。

         public void StartNormalMarketing(int shopId, List<int> marketingIdList)
{ using (SendEventMarketingService.DistributeServiceClient client = new SendEventMarketingService.DistributeServiceClient())
{
try
{ client.StartByMarketingIDList(shopId, marketingIdList, SendEventMarketingService.MarketingType.NormalMarketing); client.Close();
}
catch (Exception ex)
{
LogHelper.WriteLog("常规营销活动开启服务", ex); client.Abort();
}
}
}

而且乍一看这段代码和文中开头那一段代码貌似实现一样,但是某些人的“最佳实践”却不是这样,所以确实会导致我这样的后来人犯迷糊,对吧。。。反正我就是头晕,

简直就是弄糊涂到什么时候该用close,什么时候该用abort。。。

      

二:探索原理

  为了弄明白到底可不可以用一个try catch来替代之,下面我们一起研究一下。

1.  从代码注释角度甄别

    从类库的注释中,可以比较有意思的看出,abort方法仅仅比close多一个“立即”,再无其他,有意思,不过这对我来说并没有什么卵用,因为这个注释太

笼统了,为了让自己更加彻底的明白,只能来翻看下close和abort的源代码。

2.  从源码角度甄别

  为了方便让ILSpy调试Client代码,现在我决定用ChannelFactory来代替,如下图:

 namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ChannelFactory<IHomeService> factory = new ChannelFactory<IHomeService>(); try
{
var channel = factory.CreateChannel(); factory.Close();
}
catch (Exception ex)
{
factory.Abort();
}
}
}
}

为了让大家更好的理解,我把close方法的源码提供如下:

 // System.ServiceModel.Channels.CommunicationObject
[__DynamicallyInvokable]
public void Close(TimeSpan timeout)
{
if (timeout < TimeSpan.Zero)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("timeout", SR.GetString("SFxTimeoutOutOfRange0")));
}
using ((DiagnosticUtility.ShouldUseActivity && this.TraceOpenAndClose) ? this.CreateCloseActivity() : null)
{
CommunicationState communicationState;
lock (this.ThisLock)
{
communicationState = this.state;
if (communicationState != CommunicationState.Closed)
{
this.state = CommunicationState.Closing;
}
this.closeCalled = true;
}
switch (communicationState)
{
case CommunicationState.Created:
case CommunicationState.Opening:
case CommunicationState.Faulted:
this.Abort();
if (communicationState == CommunicationState.Faulted)
{
throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
}
goto IL_174;
case CommunicationState.Opened:
{
bool flag2 = true;
try
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
this.OnClosing();
if (!this.onClosingCalled)
{
throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this);
}
this.OnClose(timeoutHelper.RemainingTime());
this.OnClosed();
if (!this.onClosedCalled)
{
throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosed"), Guid.Empty, this);
}
flag2 = false;
goto IL_174;
}
finally
{
if (flag2)
{
if (DiagnosticUtility.ShouldTraceWarning)
{
TraceUtility.TraceEvent(TraceEventType.Warning, , SR.GetString("TraceCodeCommunicationObjectCloseFailed", new object[]
{
this.GetCommunicationObjectType().ToString()
}), this);
}
this.Abort();
}
}
break;
}
case CommunicationState.Closing:
case CommunicationState.Closed:
goto IL_174;
}
throw Fx.AssertAndThrow("CommunicationObject.BeginClose: Unknown CommunicationState");
IL_174:;
}
}

然后我提供一下Abort代码:

 // System.ServiceModel.Channels.CommunicationObject
[__DynamicallyInvokable]
public void Abort()
{
lock (this.ThisLock)
{
if (this.aborted || this.state == CommunicationState.Closed)
{
return;
}
this.aborted = true;
this.state = CommunicationState.Closing;
}
if (DiagnosticUtility.ShouldTraceInformation)
{
TraceUtility.TraceEvent(TraceEventType.Information, , SR.GetString("TraceCodeCommunicationObjectAborted", new object[]
{
TraceUtility.CreateSourceString(this)
}), this);
}
bool flag2 = true;
try
{
this.OnClosing();
if (!this.onClosingCalled)
{
throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this);
}
this.OnAbort();
this.OnClosed();
if (!this.onClosedCalled)
{
throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosed"), Guid.Empty, this);
}
flag2 = false;
}
finally
{
if (flag2 && DiagnosticUtility.ShouldTraceWarning)
{
TraceUtility.TraceEvent(TraceEventType.Warning, , SR.GetString("TraceCodeCommunicationObjectAbortFailed", new object[]
{
this.GetCommunicationObjectType().ToString()
}), this);
}
}
}

仔细观察完这两个方法,你会发现什么呢???至少我可以提出下面四个问题:

1:Abort是Close的子集吗?

   是的,因为如果你看懂了Close,你会发现Close只针对Faulted 和Opened做了判断,而其中在Faulted的枚举下会调用原生的Abort方法。。。如下图

2:我能监视Client的各种状态吗?比如Created,Opening,Fault,Closed等等。。。

   当然可以了,wcf的信道老祖宗就是ICommunicationObject,而它就有5种监听事件,这些就可以随时监听,懂伐???

         static void Main(string[] args)
{
ChannelFactory<IHomeService> factory = new ChannelFactory<IHomeService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:1920/HomeServie")); try
{
factory.Opened += (o, e) =>
{
Console.WriteLine("Opened");
}; factory.Closing += (o, e) =>
{
Console.WriteLine("Closing");
}; factory.Closed += (o, e) =>
{
Console.WriteLine("Closed");
}; var channel = factory.CreateChannel(); var result = channel.Update(new Student() { }); factory.Close();
}
catch (Exception ex)
{
factory.Abort();
}
}

3:Abort会抛出异常吗?

  

从这个截图中可以看到非常有意思的一段,那就是居然abort活生生的把异常给吞了。。。骨头都不给吐出来。。。真tmd的神奇到家了,想想也有道理,因为只有

这样,我们上层的代码在catch中才不会二次抛出“未处理异常”了,对吧,再转念看一下Close方法。

从上面图中可以看到,Close在遇到Faulted之后调用Abort方法,如果说Abort方法调用失败,Close方法会再次判断状态,如果还是Faulted的话,就会向上抛出

异常。。。这就是为什么Abort不会抛异常,Close会的原因,所以Close千万不要放在Catch块中。

4. Abort代码大概都干了些什么

  这个问题问的好,要能完美解决的话,我们看下代码,如下图,从图中可以看到,Abort的大目的就是用来关闭信道,具体会经过closeing,abort和closed这

三个方法,同时,这三个事件也会被老祖宗ICommunicationObject监听的到。

好了,最后我们关注的一个问题在于下面这条语句是否应该放在Try块中???

  ChannelFactory<IHomeService> factory = new ChannelFactory<IHomeService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:1920/HomeServie"));

很简单,我们简要的看一下代码,看里面是否会有“异常”抛出即可。。。。

可以看到,在new的过程中可能,或许会有异常的产生,所以最好把try catch改成下面这样。。。

     class Program
{
static void Main(string[] args)
{
ChannelFactory<IHomeService> factory = null;
try
{
factory = new ChannelFactory<IHomeService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:1920/HomeServie")); var channel = factory.CreateChannel(); var result = channel.Update(new Student() { }); factory.Close(); throw new Exception();
}
catch (Exception ex)
{
if (factory != null)
factory.Abort();
}
}
}

好了,综合我上面所说的一切,我个人觉得最好的方式应该是上面这样,夜深了,睡觉了,晚安。

[转]十五天精通WCF——第七天 Close和Abort到底该怎么用才对得起观众的更多相关文章

  1. 十五天精通WCF——第七天 Close和Abort到底该怎么用才对得起观众

    一:文起缘由 写这一篇的目的源自于最近看同事在写wcf的时候,用特别感觉繁琐而且云里雾里的嵌套try catch来防止client抛出异常,特别感觉奇怪,就比如下面的代码. public void S ...

  2. 十五天精通WCF——第一天 三种Binding让你KO80%的业务

    转眼wcf技术已经出现很多年了,也在.net界混的风生水起,同时.net也是一个高度封装的框架,作为在wcf食物链最顶端的我们所能做的任务已经简单的不能再简单了, 再简单的话马路上的大妈也能写wcf了 ...

  3. 十五天精通WCF——第十四天 一起聊聊FaultException

     我们在玩web编程的时候,可能你会不经意的见到一些http500的错误,我想你应该不会陌生的,原因你应该也知道,服务器异常嘛, 这时候clr会把这个未处理的异常抛给iis并且包装成http500的错 ...

  4. [转]十五天精通WCF——第十四天 一起聊聊FaultException

     我们在玩web编程的时候,可能你会不经意的见到一些http500的错误,我想你应该不会陌生的,原因你应该也知道,服务器异常嘛, 这时候clr会把这个未处理的异常抛给iis并且包装成http500的错 ...

  5. [转]十五天精通WCF——第一天 三种Binding让你KO80%的业务

    转眼wcf技术已经出现很多年了,也在.net界混的风生水起,同时.net也是一个高度封装的框架,作为在wcf食物链最顶端的我们所能做的任务已经简单的不能再简单了, 再简单的话马路上的大妈也能写wcf了 ...

  6. 十五天精通WCF——终结篇 那些你需要注意的坑

    终于一路走来,到了本系列的最后一篇了,这一篇也没什么好说的,整体知识框架已经在前面的系列文章中讲完了,wcf的配置众多,如果 不加一些指定配置,你可能会遇到一些灾难性的后果,快来一睹为快吧. 一: 第 ...

  7. 十五天精通WCF——第十三天 用WCF来玩Rest

    在我们玩wcf的时候,都会潜意识的觉得wcf就是通过soap协议交换消息的,并且可以在basic,tcp,msmq等等绑定中任意切换, 牛逼的一塌糊涂,但是呢,如果说哪一天wcf不再使用soap协议, ...

  8. 十五天精通WCF——第十二天 说说wcf中的那几种序列化

    我们都知道wcf是由信道栈组成的,在我们传输的参数走到传输信道层之前,先需要经过序列化的过程,也就是将参数序列化为message,这篇 我们就来说说这里的序列化,蛮有意思的,可能初学者也明白,在wcf ...

  9. 十五天精通WCF——第十一天 如何对wcf进行全程监控

    说点题外话,我们在玩asp.net的时候,都知道有一个叼毛玩意叫做“生命周期”,我们可以用httpmodule在先于页面的page_load中 做一些拦截,这样做的好处有很多,比如记录日志,参数过滤, ...

随机推荐

  1. css的一些命名规范

    网页制作中规范使用DIV+CSS命名规则,可以改善优化功效特别是团队合作时候可以提供合作制作效率,具体DIV CSS命名规则CSS命名大全内容篇. 常用DIV+CSS命名大全集合,即CSS命名规则 D ...

  2. 在JavaScript中"+"什么时候是链接符号,什么时候是加法运算?

    二元加法运算符“+”在两个操作数都是数字或都是字符串时,计算结果是显而易见的.加号“+”的转换规则优先考虑字符串连接,如果其中一个操作数是字符串或者转换为字符串的对象,另外一个操作数会转换为字符串,加 ...

  3. Kubernetes 配置 Taint 和 Toleration(污点和容忍)

    通过污点和容忍让pod运行在特定节点上 参考官网:https://k8smeetup.github.io/docs/concepts/configuration/taint-and-toleratio ...

  4. Elasticsearch之sense插件的安装(图文详解)

    sense插件可以方便的执行rest请求,但是中文输入的体验不是很好. 安装sense只需要在Kibana端安装插件即可,插件会自动安装到kibana的应用菜单中. [hadoop@master ki ...

  5. Spring Cloud (8) 服务容错保护-Hystrix依赖隔离

    依赖隔离 docker使用舱壁模式来实现进程的隔离,使容器与容器之间不会互相影响.而Hystrix则使用该模式实现线程池的隔离,它会为每一个Hystrix命令创建一个独立的线程池,这样就算在某个Hys ...

  6. cocos2dx实现单机版三国杀(一)

    首先需要一个UI交互类 GameUI   -layer 一个游戏驱动类,负责游戏逻辑的循环 暂时定为GameScene- scene GameScene obj 调用update 更新游戏,addch ...

  7. Android 基础知识图谱

    四大组件 Activity Service BroadcastReceiver ContentProvider Application 常用组件 Fragment RecyclerView WebVi ...

  8. CDR软件-CorelDRAW软件下载,618活动

    618我有诚意,你呢? 不花钱的618,是残缺的618 给自己一个放肆shopping的机遇 活动力度不够大? 继续升级,终极体验 618疯狂倒计时! 同志们,如果你错过了之前的抢先购和升级活动 那么 ...

  9. .htaccess使用

    RewriteEngine on #请求替换 #test-zhangsan-20 替换为 test.php?name=zhangsan&age=20 RewriteRule test-([a- ...

  10. IDEA生成增强for循环

    itar 生成array for代码块 for (int i = 0; i < array.length; i++) { = array[i]; } itco 生成Collection迭代 fo ...