WCF - 服務實例管理模式

WCF 提供了三種實例上下文模式:PreCall、PreSession 以及 Single。開發人員通過 ServiceBehavior.InstanceContextMode 就可以很容易地控制服務對象的實例管理模式。而當 WCF 釋放服務對象時,會檢查該對象是否實現了 IDisposable 接口,并調用其 Dispose 方法,以便及時釋放相關資源,同時也便于我們觀察對象釋放行為。

1. PreCall

在 PreCall 模式下,即便使用同一個代理對象,也會為每次調用創建一個服務實例。調用結束后,服務實例被立即釋放(非垃圾回收)。對于不支持 Session 的 Binding,如 BasicHttpBinding,其缺省行為就是 PreCall。
[ServiceContract]
public interface IMyService
{[OperationContract]void Test();
}[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyServie : IMyService, IDisposable
{public MyServie(){Console.WriteLine("Constructor:{0}", this.GetHashCode());}[OperationBehavior]public void Test(){Console.WriteLine("Test:{0}", OperationContext.Current.SessionId);}public void Dispose(){Console.WriteLine("Dispose");}
}public class WcfTest
{public static void Test(){AppDomain.CreateDomain("Server").DoCallBack(delegate{ServiceHost host = new ServiceHost(typeof(MyServie), new Uri("http://localhost:8080/MyService"));host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "");host.Open();});//-----------------------IMyService channel = ChannelFactory<IMyService>.CreateChannel(new WSHttpBinding(),new EndpointAddress("http://localhost:8080/MyService"));using (channel as IDisposable){channel.Test();channel.Test();}}
}

輸出:
Constructor:30136159
Test:urn:uuid:df549447-52ba-4c54-9432-31a7a533d9b4
Dispose
Constructor:41153804
Test:urn:uuid:df549447-52ba-4c54-9432-31a7a533d9b4
Dispose

2. PreSession

PreSession 模式需要綁定到支持 Session 的 Binding 對象。在客戶端代理觸發終止操作前,WCF 為每個客戶端維持同一個服務對象,因此 PreSession 模式可用來保持調用狀態。也正因為如此,PreSession 在大并發服務上使用時要非常小心,避免造成服務器過度負擔。雖然支持 Session 的 Binding 對象缺省就會啟用 PreSession 模式,但依然建議你強制指定 SessionMode.Required 和 InstanceContextMode.PerSession。
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IMyService
{[OperationContract]void Test();
}[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyServie : IMyService, IDisposable
{public MyServie(){Console.WriteLine("Constructor:{0}", this.GetHashCode());}[OperationBehavior]public void Test(){Console.WriteLine("Test:{0}", OperationContext.Current.SessionId);}public void Dispose(){Console.WriteLine("Dispose");}
}public class WcfTest
{public static void Test(){AppDomain.CreateDomain("Server").DoCallBack(delegate{ServiceHost host = new ServiceHost(typeof(MyServie), new Uri("http://localhost:8080/MyService"));host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "");host.Open();});//-----------------------IMyService channel = ChannelFactory<IMyService>.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:8080/MyService"));using (channel as IDisposable){channel.Test();channel.Test();}}
}

輸出:
Constructor:30136159
Test:urn:uuid:2f01b61d-40c6-4f1b-a4d6-4f4bc3e8847a
Test:urn:uuid:2f01b61d-40c6-4f1b-a4d6-4f4bc3e8847a
Dispose

3. Single

一如其名,服務器會在啟動時,創建一個唯一(Singleton)的服務對象。這個對象為所有的客戶端服務,并不會隨客戶端終止而釋放。
[ServiceContract]
public interface IMyService
{[OperationContract]void Test();
}[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class MyServie : IMyService, IDisposable
{public MyServie(){Console.WriteLine("Constructor:{0}; {1}", DateTime.Now, this.GetHashCode());}[OperationBehavior]public void Test(){Console.WriteLine("Test:{0}; {1}", DateTime.Now, OperationContext.Current.SessionId);}public void Dispose(){Console.WriteLine("Dispose:{0}", DateTime.Now);}
}public class WcfTest
{public static void Test(){AppDomain.CreateDomain("Server").DoCallBack(delegate{ServiceHost host = new ServiceHost(typeof(MyServie), new Uri("http://localhost:8080/MyService"));host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "");host.Open();});//-----------------------for (int i = 0; i < 2; i++){IMyService channel = ChannelFactory<IMyService>.CreateChannel(new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/MyService"));using (channel as IDisposable){channel.Test();channel.Test();}}}
}

輸出:

Constructor:2007-4-17 17:31:01; 63238509
Test:2007-4-17 17:31:03;
Test:2007-4-17 17:31:03;
Test:2007-4-17 17:31:03;
Test:2007-4-17 17:31:03;

還有另外一種方式來啟動 Single ServiceHost。
AppDomain.CreateDomain("Server").DoCallBack(delegate
{MyServie service = new MyServie();ServiceHost host = new ServiceHost(service, new Uri("http://localhost:8080/MyService"));host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "");host.Open();
});

此方式最大的好處是允許我們使用非默認構造,除此之外,和上面的例子并沒有什么區別。

需要特別注意的是,缺省情況下,Single 會對服務方法進行并發控制。也就是說,多個客戶端需要排隊等待,直到排在前面的其他客戶端調用完成后才能繼續。看下面的例子。
[ServiceContract]
public interface IMyService
{[OperationContract]void Test();
}[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class MyServie : IMyService, IDisposable
{public MyServie(){Console.WriteLine("Constructor:{0}", this.GetHashCode());}[OperationBehavior]public void Test(){Console.WriteLine("Test:{0}", OperationContext.Current.SessionId);Thread.Sleep(2000);Console.WriteLine("Test End:{0}", OperationContext.Current.SessionId);}public void Dispose(){Console.WriteLine("Dispose");}
}public class WcfTest
{public static void Test(){AppDomain.CreateDomain("Server").DoCallBack(delegate{ServiceHost host = new ServiceHost(typeof(MyServie), new Uri("http://localhost:8080/MyService"));host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "");host.Open();});//-----------------------for (int i = 0; i < 2; i++){new Thread(delegate(){IMyService channel = ChannelFactory<IMyService>.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:8080/MyService"));using (channel as IDisposable){while (true){channel.Test();}}}).Start();}}
}

輸出:
Constructor:63238509
Test:urn:uuid:d613cfce-d454-40c9-9f4d-62b30a93ff27
Test End:urn:uuid:d613cfce-d454-40c9-9f4d-62b30a93ff27
Test:urn:uuid:313de31e-96b8-47c2-8cf3-7ae743236dfc
Test End:urn:uuid:313de31e-96b8-47c2-8cf3-7ae743236dfc
Test:urn:uuid:d613cfce-d454-40c9-9f4d-62b30a93ff27
Test End:urn:uuid:d613cfce-d454-40c9-9f4d-62b30a93ff27
Test:urn:uuid:313de31e-96b8-47c2-8cf3-7ae743236dfc
Test End:urn:uuid:313de31e-96b8-47c2-8cf3-7ae743236dfc
Test:urn:uuid:d613cfce-d454-40c9-9f4d-62b30a93ff27
Test End:urn:uuid:d613cfce-d454-40c9-9f4d-62b30a93ff27
Test:urn:uuid:313de31e-96b8-47c2-8cf3-7ae743236dfc
Test End:urn:uuid:313de31e-96b8-47c2-8cf3-7ae743236dfc
...

我們可以通過修改并發模式(ConcurrencyMode)來改變這種行為,但需要自己維護多線程安全。
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)]
public class MyServie : IMyService, IDisposable
{//...
}

輸出:
Constructor:10261382
Test:urn:uuid:2bdac798-f774-40f2-b8f9-58de8d599d3c
Test:urn:uuid:9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test End:urn:uuid:2bdac798-f774-40f2-b8f9-58de8d599d3c
Test:urn:uuid:2bdac798-f774-40f2-b8f9-58de8d599d3c
Test End:urn:uuid:9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test:urn:uuid:9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test End:urn:uuid:2bdac798-f774-40f2-b8f9-58de8d599d3c
Test:urn:uuid:2bdac798-f774-40f2-b8f9-58de8d599d3c
Test End:urn:uuid:9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test:urn:uuid:9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test End:urn:uuid:2bdac798-f774-40f2-b8f9-58de8d599d3c
Test:urn:uuid:2bdac798-f774-40f2-b8f9-58de8d599d3c
Test End:urn:uuid:9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test:urn:uuid:9a328d0e-8df7-4885-94ed-e04ceedc5a09

轉載于:https://www.cnblogs.com/lzjsky/archive/2011/04/01/2002285.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275574.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275574.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275574.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

oracle io lost,磁盤IO故障

測試工作正在如火如荼的進行&#xff0c;突然數據庫就連接不上了。我連接上主機發現數據庫alert_sid日志中有如下信息&#xff1a;KCF: write/open error block0x9a6 online1file2 /oracle_data1/UNDOTBS3.dbferror27072 txt: Linux Error: 5: Input/output errorAdditional in…

易思匯完成近億元B輪融資,信中利投資

3月19日消息&#xff0c;近日&#xff0c;留學生在線付費平臺易思匯宣布已在3月份完成由信中利投資的近億元B輪融資。 易思匯聯合創始人高宇同表示&#xff0c;本輪融資將主要用于留學生信用卡、留學家庭金融商城等新產品布局&#xff0c;以及擴大團隊和市場投入。 易思匯成立…

遠程連接 錯誤 內部錯誤_關于錯誤的性質和原因。 了解錯誤因素

遠程連接 錯誤 內部錯誤Back in 2012, I was a young[er] product designer working in a small tech agency in Valencia, Spain. In parallel, I worked as a freelancer on several side projects for different clients. One day I was contacted by a new health services…

得到鵝廠最新前端開發手冊一份

又逢金九銀十&#xff0c;拿到大廠offer一直是程序員朋友的目標&#xff0c;但是去大廠就得拿出實力來。除了需要積累技術&#xff0c;了解并掌握面試的技巧&#xff0c;熟悉大廠面試流程&#xff0c;也必不可少。這里分享一份最新入職騰訊的前端社招面經&#xff0c;來看看鵝廠…

性能測試分析之帶寬瓶頸的疑惑

第一部分&#xff0c; 測試執行 先看一圖&#xff0c;再看下文 這個當然就是壓力過程中帶寬的使用率了&#xff0c;我們的帶寬是1Gbps的&#xff0c;合計傳輸速率為128MB/s&#xff0c;也正因為這個就讓我越來越疑惑了&#xff0c;不過通過壓力過程中的各項數據我又不得不相信。…

Android 中的LayoutInflater的理解

LayoutInflater與findViewById的區別&#xff1f; 對于一個已經載入的界面&#xff0c;就可以使用findViewById()方法來獲得其中的界面元素。對于一個沒有被載入或者想要動態載入的界面&#xff0c;就需要使用LayoutInflater對象的inflate()方法來載入。findViewById()是查找已…

linux rootfs編譯進內核,九鼎x6818開發板筆記:uboot、kernel、rootfs編譯和燒寫

下面記錄了如何搭建嵌入開發環境&#xff0c;如何編譯uboot、kernel、和文件系統&#xff0c;如何燒寫鏡像以及如何配置uboot環境變量。閱讀注意&#xff1a;記錄中(Base框中的內容)一些操作故意被添加&#xff0c;為了展示文件內容&#xff0c;故意調用cat(Ubuntu)或者type(wi…

figma下載_素描vs Figma困境

figma下載I distinctly remember how much hatred I had in my heart when I lived through my first UI update. The year was 2009; I had just gotten my braces off and I was ready to smash that ‘Like’ button on my high school crush’s status when I logged into …

祝大家七夕快樂,邀你源碼共讀,順帶發點紅包

大家好&#xff0c;我是若川。這是一個普通的周六。只不過又叫七夕節&#xff0c;祝大家七夕節快樂~所以就不更新技術文了。估計還是有很多讀者不知道我。若川名字由來是取自&#xff1a;上善若水&#xff0c;海納百川。順便放兩篇文章。我讀源碼的經歷&#xff0c;跟各位讀者朋…

windows 系統監視器 以及建議閥值

windows 系統監視器 以及建議閥值 計數器的說明可以在添加計數器那邊 資源 對象\計數器建議的閾值注釋磁盤Physical Disk\% Free SpaceLogical Disk\% Free Space15%磁盤Physical Disk\% Disk Time Logical Disk\% Disk Time90%磁盤Physical Disk\Disk Reads/sec、Physical Dis…

前端人員如何在linux服務器上搭建npm私有庫

為什么要搭建npm私有庫&#xff1f; 為了方便下載時&#xff0c;公共包走npmjs,私有包走內部服務器。npm包下載的速度較慢&#xff0c;搭建npm私有庫之后&#xff0c;會先操作私有庫中是否有緩存&#xff0c;有緩存直接走緩存&#xff0c;而不用重新再去請求一遍網絡。哪種方式…

硬幣 假硬幣 天平_小東西叫硬幣

硬幣 假硬幣 天平During the last 1,5 years, I’ve been traveling a lot. Apart from my must-have things like laptop, sketchbook, and power bank, there constantly appears a new one, in a familiar shape but a new look. That’s 在過去的1.5年中&#xff0c;我經常…

Linux創建一個用戶時分配組,useradd和groupadd(Linux創建用戶\用戶組\設置\分配用戶權限)的使用...

前言&#xff1a;man useradd    man groupadd    info useradd    info groupadd 都可以獲取相關命令的用法信息。個人比較喜歡讀英文解釋文檔&#xff0c;沒有你想象的那么complicated&#xff01;&#x1f61c;USERADD(8) System Management Commands USERADD…

尤雨溪發布的Vue 3.2 有哪些新變化?

大家好&#xff0c;我是若川。今天分享一篇 Vue 3.2 版本的文章。查看源碼等系列文章。學習源碼整體架構系列、年度總結、JS基礎系列1前言8.10號凌晨&#xff0c;尤雨溪在微博平臺官宣 Vue 3.2 版本正式發布&#xff1a;此版本包含一系列重要的新功能與性能改進&#xff0c;但并…

對象的清除

調用System.gc() 請求垃圾回收的最簡單的方法&#xff0c;但是注意——只是請求&#xff0c;在調用System.gc()之后&#xff0c;有可能會釋放出更多的內存空間。轉載于:https://www.cnblogs.com/happykakeru/archive/2011/04/09/2010030.html

https://zeplin.io/ 設計圖標注及切圖

2019獨角獸企業重金招聘Python工程師標準>>> https://zeplin.io/ 轉載于:https://my.oschina.net/soho00147/blog/3025646

更好的設計接口_設計可以而且必須做得更好

更好的設計接口We live in a world that becomes more dependent on technology every day. Tech gives us new ways to communicate, learn, work, and play, and recently it enabled us to reveal the appalling police brutality towards black people in the US by sharin…

linux隱寫文件剝離,雜項的基本解題思路(1)——文件操作隱寫、圖片隱寫

文件操作隱寫圖片隱寫壓縮文件處理流量取證技術文章本來是分成4部分的&#xff0c;但是前兩部分何在一起寫了也就沒有分開&#xff0c;所以干脆就只分了兩部分文件基本類型的識別一、kail 下file 文件名原理就是識別文件文件頭比如這個軟件&#xff1a;二、WinHex通過winhex分析…

賬務管理系統

2011-04-11 21:55最近寫了一個賬務管理系統&#xff08;個人版&#xff09;使用C#語言編寫&#xff0c;編譯器VS2010&#xff0c;數據庫Access2010&#xff0c;系統采用三層架構&#xff0c;界面可以換膚&#xff0c; 窗體按鈕可以移動&#xff0c;可以自定義皮膚&#xff0c;保…

初學者也能看懂的 Vue3 源碼中那些實用的基礎工具函數

1. 前言大家好&#xff0c;我是若川。最近組織了源碼共讀活動。每周讀 200 行左右的源碼。很多第一次讀源碼的小伙伴都感覺很有收獲&#xff0c;感興趣可以加我微信ruochuan12&#xff0c;拉你進群學習。寫相對很難的源碼&#xff0c;耗費了自己的時間和精力&#xff0c;也沒收…