WCF 学习总结3 -- 实例模式

通过WCF的ServiceBehaviorAttribute设定InstanceContextMode有下面的3中模式: 
1. Single —— 表示所有的客户端共享一个会话(服务对象)(服务关闭时才会销毁服务对象) 
2. PerCall —— 表示每次调用都会创建一个会话(服务对象)(调用完毕后就会销毁服务对象) 
3. PerSession —— 表示为每个连接(每个客户端代理对象) 创建一个会话(服务对象),只有指定IsTerminating=true的操作被调用,或者是设定的SessionTimeout超时的时候,服务对象会被销毁。但支持Session的Binding只有:WSHttpBinding、WSDualHttpBinding、WSFederationHttpBinding、NetTcpBinding。 

测试一下上述行为,设计3个Service,每个Service都在构造函数中输出内容并实现IDispose接口,在Dispose()里输出内容。 
(准备) Host实现代码:

[c-sharp] view plain copy
 
  1. static void Main(string[] args)  
  2. {  
  3.     ServiceHost host1 = new ServiceHost(typeof(SingleService));  
  4.     host1.Open();  
  5.     Console.WriteLine("SingleService Opened!");  
  6.     ServiceHost host2 = new ServiceHost(typeof(PeerCallService));  
  7.     host2.Open();  
  8.     Console.WriteLine("PeerCallService Opened!");  
  9.     ServiceHost host3 = new ServiceHost(typeof(PeerSessionService));  
  10.     host3.Open();  
  11.     Console.WriteLine("PeerSessionService Opened!");  
  12.     Console.Read();  
  13. }  

 


(一) Single服务契约——服务端启动时实例化

 

[c-sharp] view plain copy
 
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]  
  2. public class SingleService : IService1, IDisposable  
  3. {  
  4.     public SingleService()  
  5.     {  
  6.         Console.WriteLine("SingleService ({0}) [{1:mm:ss.fff}] ctor", this.GetHashCode(), DateTime.Now);  
  7.     }  
  8.     public string GetData(string value)  
  9.     {  
  10.         return string.Format("SingleService ({0}) GetData [{1}]", this.GetHashCode(), value);  
  11.     }  
  12.     public void Dispose()  
  13.     {  
  14.         Console.WriteLine("SingleService ({0}) [{1:mm:ss.fff}] dispose", this.GetHashCode(), DateTime.Now);  
  15.     }  
  16. }  


启动Host调用SingleService.GetData,服务端的输出:

上图说明SingleService在服务启动时就创建了。

 


(二) PerCall服务契约——每次调用都重新实例化

[c-sharp] view plain copy
 
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]  
  2. public class PeerCallService : IService1, IDisposable   
  3. {  
  4.     public PeerCallService()  
  5.     {  
  6.         Console.WriteLine("PeerCallService ({0}) [{1:mm:ss.fff}] ctor", this.GetHashCode(), DateTime.Now);  
  7.     }  
  8.     public string GetData(string value)  
  9.     {  
  10.         return string.Format("PeerCallService ({0}) GetData [{1}]", this.GetHashCode(), value);  
  11.     }  
  12.     public void Dispose()  
  13.     {  
  14.         Console.WriteLine("PeerCallService ({0}) [{1:mm:ss.fff}] dispose", this.GetHashCode(), DateTime.Now);  
  15.     }  
  16. }  


调用PeerCallService.GetData

 

 

(三) PerSession服务契约——客户端第一次调用时实例化

[c-sharp] view plain copy
 
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  
  2. public class PeerSessionService : IService1, IDisposable  
  3. {  
  4.     public PeerSessionService()  
  5.     {  
  6.         Console.WriteLine("PeerSessionService ({0}) [{1:mm:ss.fff}] ctor", this.GetHashCode(), DateTime.Now);  
  7.     }  
  8.     public string GetData(string value)  
  9.     {  
  10.         return string.Format("PeerSessionService ({0}) GetData [{1}]", this.GetHashCode(), value);  
  11.     }  
  12.     public void Dispose()  
  13.     {  
  14.         Console.WriteLine("PeerSessionService ({0}) [{1:mm:ss.fff}] dispose", this.GetHashCode(), DateTime.Now);  
  15.     }  
  16. }  

 

服务端的Session的Timeout是由binding属性receiveTimeout和inactivityTimeout控制的。

[xhtml] view plain copy
 
  1. <bindings>  
  2.   <wsHttpBinding>  
  3.     <binding name="wsHttp" receiveTimeout="00:00:15">  
  4.       <reliableSession enabled="true"  inactivityTimeout="00:00:15" />  
  5.     </binding>  
  6.   </wsHttpBinding>  
  7. </bindings>  

 

关于这两个属性的配置,更详细的说明请参考MSDN,按照配置服务对象在客户端没有调用后15秒销毁:

需要注意的是,每次 new WcfSvcPeerSession.Service1Client() 都是一个新的Session。

接下来看看怎么通过 OperationContact 的 IsInitiating 和 IsTerminating 来控制Session。IsInitiating 表示该方法是否可以初始化 Session,IsTerminating 表示该方法是否可以终止 Session。默认设置 IsInitiating=true,IsTerminating=false。另外通过IsInitiating和IsTerminating 控制的时候,必须设置 ServiceContract 的 SessionMode.Required 
服务契约的定义

[c-sharp] view plain copy
 
  1. [ServiceContract(SessionMode = SessionMode.Required)]  
  2. interface IService2  
  3. {  
  4.     [OperationContract(IsInitiating=true)]  
  5.     void LogIn();  
  6.     [OperationContract]  
  7.     void DoSth();  
  8.     [OperationContract(IsTerminating=true)]  
  9.     void LogOff();  
  10. }  


服务契约的实现:

[c-sharp] view plain copy
 
  1. public class PeerSessionService2 : IService2  
  2. {  
  3.     public void LogIn()  
  4.     {  
  5.         var sessionId = OperationContext.Current.SessionId;  
  6.         Console.WriteLine("{0} LogIn.", sessionId);  
  7.     }  
  8.     public void DoSth()  
  9.     {  
  10.         var sessionId = OperationContext.Current.SessionId;  
  11.         Console.WriteLine("{0} DoSth.", sessionId);  
  12.     }  
  13.     public void LogOff()  
  14.     {  
  15.         var sessionId = OperationContext.Current.SessionId;  
  16.         Console.WriteLine("{0} LogOff.", sessionId);  
  17.     }  
  18. }  

 

一旦LogOff即IsTerminating,Session就结束了。再次调用任何服务端方法都会引发异常。

posted on 2016-03-25 12:24  大西瓜3721  阅读(229)  评论(0编辑  收藏  举报

导航