WCF Session開啟服務契約
WCF開發工具在實際應用中,可以幫助我們實現許多比較重要的功能需求。在這篇文章中,我們會為大家詳細講解一下WCF Session的相關應用技術,并從中加深對WCF功能的了解程度。#t#
WCF Session 特點:
由調用程序(Calling Application)發起初始化和終止操作。
由具體的 Binding 類型實現,因此它們之間的細節可能有所不同。
不提供 ASP.NET Session 那樣的數據容器。
啟動WCF Session的方法包括:
調用 Channel 的 Open 方法。我們可以使用 ChannelFactory 來創建 Channel。
調用客戶端代理對象的 Open 方法(ClientBase.Open)。
調用任何允許初始化會話服務方法(缺省情況下所有的服務方法都自動初始化Session,也就是 IsInitiating=true)。
結束WCF Session的方法包括:
調用 Channel 的 Close 方法。
調用客戶端代理對象的 Close 方法(ClientBase.Close)。
調用任何包含 "IsTerminating=true" 聲明的服務方法(缺省情況下所有的服務方法 IsTerminating=false,需要我們顯示聲明)。
開啟服務契約的WCF Session
可以選擇的模式包括:Required、Allowed、NotAllowed。Required 表示必須使用 Session,如果 Binding 不支持,則會拋出異常;Allowed 表示如果 Binding 支持 Session 則開啟會話;NotAllowed 表示停用 Session。多數 Binding 缺省就會開始 Session,而 BaseHttpBinding 不支持 Session。
- [ServiceContract(SessionModeSessionMode=SessionMode.Required)]
- public interface ICalculate
- {
- [OperationContract]
- int Add(int a, int b);
- }