兩步解決WCF狀態(tài)保存
WCF開發(fā)框架中有很多功能值得我們?nèi)ド钊氲难芯?。比如狀態(tài)保存等等。在這里我們就為大家詳細(xì)介紹有關(guān)WCF狀態(tài)保存的一些相關(guān)知識。#t#
WCF狀態(tài)保存分為兩步:
(1) 使用SessionMode 來使Session有效化
- [ServiceContract(SessionMode
SessionMode=SessionMode.Required)] - public interface ICalculator
- {
- [OperationContract(IsOneWay=true)]
- void Adds(double x);
- [OperationContract]
- double GetResult();
- }
(2)ServiceBehavior 里面利用參數(shù)InstanceContextMode設(shè)定到底使用哪一種Session方式
- [ServiceBehavior(InstanceContextMode
InstanceContextMode=InstanceContextMode.PerCall)]- public class CalculatorService:ICalculator
- {
- private double _result;
- public double Result
- {
- get { return _result; }
- set { _result = value; }
- }
- public void Adds(double x)
- {
- Console.WriteLine("The Add Method is
invoked and The current SessionID is
{0} ",OperationContext.Current.SessionId);- this._result += x;
- }
- public double GetResult()
- {
- Console.WriteLine("The GetResult Method
is invoked and The current SessionID is
{0} ", OperationContext.Current.SessionId);- return this._result;
- }
- public CalculatorService()
- {
- Console.WriteLine("CalculatorService
object has been Created ");- }
- ~CalculatorService()
- {
- Console.WriteLine("CalculatorService
object has been destoried ");- }
- }
WCF狀態(tài)保存SessionMode 有三種值:
(1)Allowed 默認(rèn)選值,允許但不強(qiáng)制使用Session
(2)Required 強(qiáng)制使用Session
(3)NotAllowed 不允許使用Session
WCF狀態(tài)保存InstanceContextMode 有三種值:
(1) Percall 為user的每一次調(diào)用生成一個SessionID
生命周期:調(diào)用開始 ---->調(diào)用結(jié)束,這種情況和不使用Session是一樣的
(2) PerSession 為每個用戶生成一個SessionID
生命周期:客戶端代理生成--->客戶端代理關(guān)閉 和最原先的Session是一樣的
(3) Seigle 生成唯一的SessionID,所有的用戶共享 從host創(chuàng)建---->host 關(guān)閉,和Application 一樣