WCF PreCal模式基本代碼示例解析
在WCF實(shí)例上下文模式中,PreCal模式是一個(gè)比較重要的模式。我們?cè)谶@篇文章中將會(huì)針對(duì)WCF PreCal模式的相關(guān)概念及應(yīng)用技巧做一個(gè)詳細(xì)的闡述,希望朋友們能從中獲得一些幫助。
在WCF PreCal模式下,即便使用同一個(gè)代理對(duì)象,也會(huì)為每次調(diào)用創(chuàng)建一個(gè)服務(wù)實(shí)例。調(diào)用結(jié)束后,服務(wù)實(shí)例被立即釋放(非垃圾回收)。對(duì)于不支持 Session 的 Binding,如 BasicHttpBinding,其缺省行為就是 PreCall。
- [ServiceContract]
- public interface IMyService
- {
- [OperationContract]
- void Test();
- }
- [ServiceBehavior(InstanceContextModeInstanceContextMode =
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
以上就是我們?yōu)榇蠹医榻B的WCF PreCal模式的相關(guān)介紹。
【編輯推薦】