WCF - IDisposable
发布日期:2021-06-29 03:54:15
浏览次数:2
分类:技术文章
本文共 1533 字,大约阅读时间需要 5 分钟。
WCF 释放服务对象时会检查该对象是否实现了 IDisposable 接口,并适时调用 Dispose 方法,以便服务对象能及时回收相关资源。但有一点要注意,Dispose 方法是被异步调用的,也就是说它被扔到系统线程池中执行,且无法获得上下文对象。看下面的例子。
[ServiceContract(SessionMode=SessionMode.Required)] public interface IContract { [OperationContract] void Test(); } [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)] public class MyService : IContract, IDisposable { public MyService() { Console.WriteLine("Constructor:{0}", OperationContext.Current.SessionId); } public void Dispose() { Console.WriteLine("Dispose..."); Console.WriteLine("Is ThreadPool Thread? {0}", Thread.CurrentThread.IsThreadPoolThread); Console.WriteLine("OperationContext = Null? {0}", OperationContext.Current == null); } public void Test() { Console.WriteLine("Test..."); } } public class WcfTest { public static void Test() { AppDomain.CreateDomain("Server1").DoCallBack(delegate { ServiceHost host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8080/MyService")); host.AddServiceEndpoint(typeof(IContract), new WSHttpBinding(), ""); ServiceThrottlingBehavior throttling = new ServiceThrottlingBehavior(); throttling.MaxConcurrentInstances = 10; host.Description.Behaviors.Add(throttling); host.Open(); }); IContract channel = ChannelFactory<IContract>.CreateChannel(new WSHttpBinding();, new EndpointAddress("http://localhost:8080/MyService")); using (channel as IDisposable) { channel.Test(); } } }
输出:Constructor:urn:uuid:4213d1bf-a2ad-478d-8eda-c209fc70b873Test...Dispose...Is ThreadPool Thread? TrueOperationContext = Null? True 转载地址:https://blog.csdn.net/zengjibing/article/details/3813418 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月24日 19时32分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Gradle for Android(四)——依赖冲突解决
2019-04-29
Gradle for Android(三)——依赖管理(二)
2019-04-29
Gradle for Android(五)——构建变体
2019-04-29
Gradle for Android(二)——build.gradle基本配置
2019-04-29
Gradle for Android(七)——一些使用技巧
2019-04-29
Android Bugs——Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside
2019-04-29
Android Bugs——RecyclerView.Adapter java.lang.IllegalStateException: The specified child already has
2019-04-29
Android内存优化——使用SparseArray和ArrayMap代替HashMap
2019-04-29
Android——仿ios阻尼回弹动画
2019-04-29
ExoPlayer实现设置画面比例功能
2019-04-29
Android——DialogFragment(一)用法介绍
2019-04-29
第二十一章:变换(十三)
2019-04-29
【数据结构】串的总结——基本知识要点汇总
2019-04-29
【数据结构】树和二叉树总结——基本知识要点汇总
2019-04-29
【数据结构】数组和广义表总结——基本知识要点汇总
2019-04-29
【数据结构】图的总结——基本知识要点汇总
2019-04-29