WCF - IsInitiating & IsTerminating
发布日期:2021-06-29 03:54:15
浏览次数:2
分类:技术文章
本文共 2429 字,大约阅读时间需要 8 分钟。
"IsInitiating=false" 表示执行该方法前,必须调用一个标注了 "IsInitiating=true" 的方法来创建 Session;而 "IsTerminating=true" 表示该方法调用结束后,WCF 将会释放服务对象,客户端代理将不能继续任何操作(除非我们创建一个新的代理)。IsInitiating 和 IsTerminating 只能用于启用了 Session 的服务对象上,缺省情况下 "IsInitiating=true, IsTerminating=false"。
我 们可以使用 IsInitiating 和 IsTerminating 来控制服务对象的流程和状态,比如使用 "IsInitiating=false" 强制客户端在调用订单服务的任何操作前,必须使用 "Init(IsInitiating=true)" 来获取一个唯一流水号,而一旦调用 "Process(IsTerminating=true)" 完成订单结算后,就不能再做任何处理。 [ServiceContract(SessionMode = SessionMode.Required)] public interface IOrderService { [OperationContract(IsInitiating = true)] void Init(); [OperationContract(IsInitiating = false, IsTerminating = false)] void DoSomething(); [OperationContract(IsInitiating = false, IsTerminating = true)] void Process(); } [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class OrderService : IOrderService { public void Init() { Console.WriteLine("Init..."); } public void DoSomething() { Console.WriteLine("DoSomething..."); } public void Process() { Console.WriteLine("Process..."); } } public class WcfTest { public static void Test() { AppDomain.CreateDomain("Server").DoCallBack(delegate { ServiceHost host = new ServiceHost(typeof(OrderService), new Uri("http://localhost:8080/MyService")); host.AddServiceEndpoint(typeof(IOrderService), new WSHttpBinding(), ""); host.Open(); }); //----------------------- IOrderService order = ChannelFactory<IOrderService>.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:8080/MyService")); using (order as IDisposable) { order.Init(); order.DoSomething(); order.Process(); } } }
如何客户端不按照这个次序调用,则会触发异常。错误1 IOrderService order = ChannelFactory<IOrderService>.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:8080/MyService")); using (order as IDisposable) { //order.Init(); order.DoSomething(); order.Process(); }
异常 未处理 System.InvalidOperationException Message="The operation 'DoSomething' cannot be the first operation to be called because IsInitiating is false."
错误2 IOrderService order = ChannelFactory<IOrderService>.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:8080/MyService")); using (order as IDisposable) { order.Init(); order.DoSomething(); order.Process(); order.DoSomething(); }
异常 未处理 System.InvalidOperationException Message="This channel cannot send any more messages because IsTerminating operation 'Process' has already been called."
转载地址:https://blog.csdn.net/zengjibing/article/details/3813174 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月28日 17时19分43秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python爬取腾讯动漫全站漫画详细教程(附带源码)
2019-04-29
python实现电商平台秒杀商品脚本程序
2019-04-29
520快乐~用python实现520祝福弹窗
2019-04-29
Python制作属于自己的有声小说
2019-04-29
Python制作专属有声小说(调用百度语音合成接口)
2019-04-29
用Python爬取淘宝2000款T T
2019-04-29
17行python代码爬取堆糖网所有MeiNv图片
2019-04-29
Python爬取15万条《我是余欢水》弹幕,还原一个丧到极致的中年人生
2019-04-29
Python 使用 PyQt5 开发的关机小工具分享
2019-04-29
可能是最全面的 python 字符串拼接总结
2019-04-29
利用Python爬取微博数据生成词云图片实例代码
2019-04-29
对Python3 解析html的几种操作方式小结
2019-04-29
Python基于opencv调用摄像头获取个人图片的实现方法
2019-04-29
Opencv+Python实现图像运动模糊和高斯模糊的示例
2019-04-29
python初学者入门学习笔记:交互式环境与print输出
2019-04-29
python初学者入门学习笔记:变量的使用
2019-04-29
python初学者入门学习笔记:字符串的操作(连接/获取长度/截取)
2019-04-29
python初学者入门学习笔记:字符串的操作(重复/转换/替换/原始字符串)
2019-04-29
python初学者入门学习笔记:字符串的操作(去除/查询/计数)
2019-04-29
python初学者入门学习笔记:字符串的操作(获取输入/格式化)
2019-04-29