WCF状态保存不同方法介绍

WCF已经推出就深受广大开发人员的喜爱。它的强大的功能可以帮助我们轻松的打造一个跨平台的安全性高的解决方案。在这里我们可以先从WCF状态保存的方法来对此进行一个分析,加深其的印象。

WCF状态保存分为两步:

(1) 使用SessionMode 来使Session有效化

 

 
 
 
  1. [ServiceContract(SessionModeSessionMode=SessionMode.Required)]  
  2. public interface ICalculator  
  3. {  
  4. [OperationContract(IsOneWay=true)]  
  5. void Adds(double x);  
  6. [OperationContract]  
  7. double GetResult();  

 

(2)ServiceBehavior 里面利用参数InstanceContextMode设定到底使用哪一种Session方式
 

 

 
 
 
  1. [ServiceBehavior(InstanceContextModeInstanceContextMode=
    InstanceContextMode.PerCall)]  
  2. public class CalculatorService:ICalculator  
  3. {  
  4. private double _result;  
  5. public double Result  
  6. {  
  7. get { return _result; }  
  8. set { _result = value; }  
  9. }  
  10. public void Adds(double x)  
  11. {  
  12. Console.WriteLine("The Add Method is invoked and The current 
    SessionID is {0} ",OperationContext.Current.SessionId);  
  13. this._result += x;  
  14. }  
  15. public double GetResult()  
  16. {  
  17. Console.WriteLine("The GetResult Method is invoked and The 
    current SessionID is {0} ", OperationContext.Current.SessionId);  
  18. return this._result;  
  19. }  
  20. public CalculatorService()  
  21. {  
  22. Console.WriteLine("CalculatorService object has been Created ");  
  23. }  
  24. ~CalculatorService()  
  25. {  
  26. Console.WriteLine("CalculatorService object has been destoried ");  
  27. }  

 

SessionMode 有三种值:#t#

(1)Allowed 默认选值,允许但不强制使用Session

(2)Required 强制使用Session

(3)NotAllowed 不允许使用Session

InstanceContextMode 有三种值:

(1) Percall 为user的每一次调用生成一个SessionID

WCF状态保存生命周期:调用开始 ---->调用结束,这种情况和不使用Session是一样的

(2) PerSession 为每个用户生成一个SessionID

生命周期:客户端代理生成--->客户端代理关闭 和最原先的Session是一样的

(3) Seigle 生成***的SessionID,所有的用户共享 从host创建---->host 关闭,和Application 一样

WCF状态保存的相关内容就为大家介绍到这里。

THE END