WCF PreSession模式保持調(diào)用狀態(tài)
作者:佚名
WCF PreSession模式的應(yīng)用,首先需要我們對其進行綁定,那么正確的操作方法將會在這篇文章中詳細給出,相信能給大家?guī)硪恍椭?/div>
WCF開發(fā)工具是.NET Framework 3.5的一個重要組成部件。它的出現(xiàn)在一定程度上改變了開發(fā)人員的編程習慣,為開發(fā)人員帶來了非常大幫助。在這里我們將會先了解到WCF PreSession模式的一些基本概念。
WCF PreSession模式需要綁定到支持 Session 的 Binding 對象。在客戶端代理觸發(fā)終止操作前,WCF 為每個客戶端維持同一個服務(wù)對象,因此 PreSession 模式可用來保持調(diào)用狀態(tài)。也正因為如此,PreSession 在大并發(fā)服務(wù)上使用時要非常小心,避免造成服務(wù)器過度負擔。雖然支持 Session 的 Binding 對象缺省就會啟用 PreSession 模式,但依然建議你強制指定 SessionMode.Required 和 InstanceContextMode.PerSession。
- [ServiceContract(SessionModeSessionMode = SessionMode.Required)]
- public interface IMyService
- {
- [OperationContract]
- void Test();
- }
- [ServiceBehavior(InstanceContextModeInstanceContextMode =
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();
- }
- }
- }
WCF PreSession模式代碼輸出:
- Constructor:30136159
- Test:urn:uuid:2f01b61d-40c6-4f1b-a4d6-4f4bc3e8847a
- Test:urn:uuid:2f01b61d-40c6-4f1b-a4d6-4f4bc3e8847a
- Dispose
以上就是我們?yōu)榇蠹医榻B的WCF PreSession模式的基本概念。
【編輯推薦】
責任編輯:曹凱
來源:
博客園


相關(guān)推薦
2009-12-22 15:55:10
2020-11-04 08:54:54
2009-11-06 13:23:27




