WCF服务实例管理模式之PreSession应用

开发 开发工具
WCF服务实例管理模式中的PreSession模式的应用,需要我们将其绑定到一个支持Swssion的Binding对象。在这里将会给出具体的操作方法。

WCF服务实例管理模式中,总共有三种应用模式可以供开发人员选择应用。今天主要就是针对其中一个比较常用的PreSession模式进行一些相关介绍。PreSession 模式需要绑定到支持 Session 的 Binding 对象。

在客户端代理触发终止操作前,WCF 为每个客户端维持同一个服务对象,因此 PreSession 模式可用来保持调用状态。也正因为如此,PreSession 在大并发服务上使用时要非常小心,避免造成服务器过度负担。虽然支持 Session 的 Binding 对象缺省就会启用 PreSession 模式,但依然建议你强制指定 SessionMode.Required 和 InstanceContextMode.PerSession。

  1. [ServiceContract(SessionMode = SessionMode.Required)]  
  2. public interface IMyService  
  3. {  
  4. [OperationContract]  
  5. void Test();  
  6. }  
  7. [ServiceBehavior(InstanceContextMode = 
    InstanceContextMode.PerSession)]  
  8. public class MyServie : IMyService, IDisposable  
  9. {  
  10. public MyServie()  
  11. {  
  12. Console.WriteLine("Constructor:{0}", this.GetHashCode());  
  13. }  
  14. [OperationBehavior]  
  15. public void Test()  
  16. {  
  17. Console.WriteLine("Test:{0}", OperationContext.Current.SessionId);  
  18. }  
  19. public void Dispose()  
  20. {  
  21. Console.WriteLine("Dispose");  
  22. }  
  23. }  
  24. public class WcfTest  
  25. {  
  26. public static void Test()  
  27. {  
  28. AppDomain.CreateDomain("Server").DoCallBack(delegate  
  29. {  
  30. ServiceHost host = new ServiceHost(typeof(MyServie), 
    new Uri("http://localhost:8080/MyService"));  
  31. host.AddServiceEndpoint(typeof(IMyService), 
    new WSHttpBinding(), "");  
  32. host.Open();  
  33. });  
  34. //-----------------------  
  35. IMyService channel = ChannelFactory<IMyService>.
    CreateChannel(new WSHttpBinding(),   
  36. new EndpointAddress("http://localhost:8080/MyService"));  
  37. using (channel as IDisposable)  
  38. {  
  39. channel.Test();  
  40. channel.Test();  
  41. }  
  42. }  

输出:

  1. Constructor:30136159  
  2. Test:urn:uuid:2f01b61d-40c6-4f1b-a4d6-4f4bc3e8847a  
  3. Test:urn:uuid:2f01b61d-40c6-4f1b-a4d6-4f4bc3e8847a  
  4. Dispose 

【编辑推荐】

  1. WCF控制服务对象释放特殊方式介绍
  2. WCF事务演示经典实例剖析
  3. 深入分析WCF事务投票实现方式
  4. WCF MSMQ队列基本概念简述
  5. PDA访问WCF实现重点在过程
责任编辑:曹凯 来源: 豆豆网
相关推荐

2010-02-24 13:42:55

WCF PreSess

2009-09-17 12:15:09

WSUS服务器

2010-02-26 14:49:10

WCF服务实例单一性

2014-06-06 09:40:14

802.11

2012-05-08 09:57:03

虚拟化应用

2023-09-28 11:42:15

2013-02-27 10:06:43

2009-11-06 09:39:40

WCF契约

2009-11-05 13:54:07

WCF Service

2009-03-17 09:49:00

2011-06-17 10:30:36

2010-02-25 17:04:54

WCF实例上下文

2012-02-14 10:18:11

WCF数据服务

2015-07-29 13:27:37

管理

2018-05-31 10:33:53

云计算

2021-08-06 06:51:14

NacosRibbon服务

2009-11-06 15:02:47

WCF契约查询

2009-11-05 16:01:51

WCF单调服务

2009-10-09 17:18:13

RHEL配置NIS

2013-01-24 13:42:05

电信行业通信网络管理模式
点赞
收藏

51CTO技术栈公众号