WF4.0 Beta1之旅(2):异常处理
1.WF3.5中主要使用FaultHandlerActivity来模拟TryCatch捕获异常,现在从WF4.0 beta1来看工具箱中已经没有了FaultHandlerActivity活动,顺序工作流也没有了错误处理视图,取而代之出现了Try/Catch,Catch<T>,Thorw活动,本文就介绍下这几个活动。关于WF3.5中对异常的处理方式,可以参考下文:http://www.cnblogs.com/carysun/archive/2008/10/18/WFException.html
2.我们依然使用顺序工作流模板,我们完成这样一个工作流,工作流的输入参数为UserName,输出参数为Greeting,工作流中会根据输入参数的不同而输出不同的信息,我们要处理的异常就是如果工作流没有提供输入的情况下。
3.工作流的参数如下:
UserName输入参数,Greeting输出参数,如下。
4.给做工作增加一个变量FirstWord.注意变量是有作用域的,即他的Scope属性。
5.下面我们看整个工作流的逻辑,工作流上面是try catch,下面是Assign,Assign将Greeting的值设置为FirstWord+“,”+UserName,如下图:
trycatch就是我们的逻辑部分。Try中就是我们要执行的程序,catches中捕获异常并处理,
6.我们先来看下Try中逻辑,我们根据工作流的输入参数UserName来判断其长度其否为偶数,来设置变量FirstWord的值,如下:
7.在Catches中我们来处理捕获到得异常,这个部分我们可以拖入多个Catch<T>活动,左边为异常类型,右边为相应处理的Action,拖入catch<T>会自动出现如下对话框选择异常类型。如下图:
这个例子中我们捕获异常后的处理方式是将异常再次抛出,我们加入一个Throw活动,并设置其Exception属性。如下图:
8.工作流的部分我们就完成了,下面是宿主程序:
class Program { static voidMain(string[] args) { AutoResetEvent syncEvent = newAutoResetEvent(false); Console.Write("Enter your name: "); stringuserName = Console.ReadLine(); stringgreeting = null; Dictionary<string, object> input = newDictionary<string, object>(); input.Add("UserName", userName); WorkflowInstance myInstance;
if(string.IsNullOrEmpty(userName)) { myInstance = newWorkflowInstance(newSequence1()); } else {
myInstance = newWorkflowInstance(newSequence1(), input); }
myInstance.OnCompleted = delegate(WorkflowCompletedEventArgs e) { if(e.CompletionState == ActivityInstanceState.Closed) { greeting = e.Outputs["Greeting"].ToString(); } else { Console.WriteLine("Workflow CompletionState is {0}", e.CompletionState); Console.ReadLine(); } syncEvent.Set(); }; myInstance.OnUnhandledException = delegate(WorkflowUnhandledExceptionEventArgs e) { Console.WriteLine(e.UnhandledException.ToString()); returnUnhandledExceptionAction.Terminate; }; myInstance.OnAborted = delegate(WorkflowAbortedEventArgs e)
{ Console.WriteLine(e.Reason); syncEvent.Set(); }; myInstance.Run(); syncEvent.WaitOne(); Console.WriteLine(greeting); Console.ReadLine(); } }
不论工作流是否发生异常都会执行OnCompleted事件,只不过e.CompletionState不同,有异常的时候为Fault,正常为Closed。
工作流也可以使用WorkflowInstance的OnUnhandledException事件来处理在TryCatch中没有处理的异常。
[TestMethod] public void IfElseHelloWorldTest() { Dictionary<string, object> input = new Dictionary<string, object>(); { { "UserName", "Cary" } }; var output = WorkflowInvoker.Invoke(new CaryWFDemo.Sequence1(), input); Assert.AreEqual("Hello,Cary", output["Greeting"]); } [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void ShouldHandleNullUserName() { WorkflowInvoker.Invoke(new CaryWFDemo.Sequence1()); }
相关文章
- 【vue3项目】解决 “TypeError: Cannot read properties of undefined (reading ‘xxx‘)“
- Nginx跨域解决方案
- Vue2.0搭建脚手架(vue-cli)
- Code For Better 谷歌开发者之声——初识Web与谷歌,拉起兴趣之心。
- 申请免费证书、域名解析以及nginx部署配置https为微信小程序服务
- 小满Vue3第四十六章(Proxy跨域)
- 计算机网络面试大总结
- 超详细纯前端导出excel并完成各种样式的修改(xlsx-style)
- day21-web开发会话技术03
- 大二C#实现酒店管理系统(C端展示、前台操作、登记入住、入住管理、职位管理、公告系统等)
- 每日算法之二叉树中和为某一值的路径(一)
- Vue2中使用axios的三种方法
- day20-web开发会话技术02
- 【Vue】Vue 项目前端响应式布局及框架搭建
- fastdfs上传图片后浏览器访问报错Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR
- Vue 2项目如何升级到Vue 3?
- WebSocket的基本使用
- 每日算法之调整数组顺序使奇数位于偶数前面(二)
- Nginx反向代理WebSocket服务连接报错:WebSocket connection to “wss://xxx/xxx“ failed
- 第2-3-8章 分片上传和分片合并的接口开发-文件存储服务系统-nginx/fastDFS/minio/阿里云oss/七牛云oss