成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

WCF會話狀態(tài)功能特點解析

開發(fā) 開發(fā)工具
WCF會話狀態(tài)主要特點比較突出,我們將會在這篇文章中為大家詳細(xì)介紹一下相關(guān)的應(yīng)用技巧,以及他的各種啟動方法等等。

WCF是一個.NET Framework 3.5的重要組成部件,可以幫助我們成功創(chuàng)建出一個安全性強的開發(fā)解決方案。我們在這里會為大家介紹一下WCF會話狀態(tài)的相關(guān)概念。WCF會話狀態(tài)和兩個端點(EndPoint)之間的一系列消息交換相關(guān)聯(lián),它實際上是 "實例上下文(Instance Context)”,控制著服務(wù)對象實例的創(chuàng)建方式和生存期。和 ASP.NET Session 有很大不同。#t#

WCF會話狀態(tài)特點:

由調(diào)用程序(Calling Application)發(fā)起初始化和終止操作。

由具體的 Binding 類型實現(xiàn),因此它們之間的細(xì)節(jié)可能有所不同。

不提供 ASP.NET Session 那樣的數(shù)據(jù)容器。

啟動WCF會話狀態(tài)的方法包括:

調(diào)用 Channel 的 Open 方法。我們可以使用 ChannelFactory 來創(chuàng)建 Channel。

調(diào)用客戶端代理對象的 Open 方法(ClientBase.Open)。

調(diào)用任何允許初始化會話服務(wù)方法(缺省情況下所有的服務(wù)方法都自動初始化Session,也就是 IsInitiating=true)。

結(jié)束WCF會話狀態(tài)的方法包括:

調(diào)用 Channel 的 Close 方法。

調(diào)用客戶端代理對象的 Close 方法(ClientBase.Close)。

調(diào)用任何包含 "IsTerminating=true" 聲明的服務(wù)方法(缺省情況下所有的服務(wù)方法 IsTerminating=false,需要我們顯示聲明)。

要使用 WCF Session,我們一般按如下步驟進(jìn)行。

 

1. 開啟服務(wù)契約的 Session。

可以選擇的模式包括:Required、Allowed、NotAllowed。Required 表示必須使用 Session,如果 Binding 不支持,則會拋出異常;Allowed 表示如果 Binding 支持 Session 則開啟會話;NotAllowed 表示停用 Session。多數(shù) Binding 缺省就會開始 Session,而 BaseHttpBinding 不支持 Session。

 

 

  1. < ServiceContract(SessionModeSessionMode:=SessionMode.Required)> _  
  2. Public Interface IService1  
  3. < OperationContract(isinitiating:=False)> _  
  4. Function GetData(ByVal value As Integer) As String  
  5. End Interface 

2. 使用 ServiceBehaviorAttribute 和 InstanceContextMode 在服務(wù)契約的實現(xiàn)類型上指定服務(wù)對象的 "實例上下文模式"。

InstanceContextMode 可選擇的方式包括:PerSession、PerCall、Single。PerSession 表示為每個連接(每個客戶端代理對象) 創(chuàng)建一個會話(服務(wù)對象);PerCall 則為每次調(diào)用(Operate)創(chuàng)建一個會話(服務(wù)對象);Single 則表示所有的客戶端共享一個會話(服務(wù)對象)。

 

 

  1. < ServiceBehavior(InstanceContextModeInstanceContextMode:
    =InstanceContextMode.PerSession)> _  
  2. Public Class Service1  
  3. Implements IService1  
  4. Public Function GetData(ByVal value As Integer) As String 
    Implements IService1.GetData  
  5. Console.WriteLine(OperationContext.Current.SessionId)  
  6. Return String.Format("You entered: {0}", value)  
  7. End Function  
  8. End Class 

 

客戶端調(diào)用:

 

 

  1. Sub Main()  
  2. Dim url As String = "http://localhost:8731/
    Design_Time_Addresses/WcfServiceLibrary1/Service1/mex"
     
  3. Dim host As New System.ServiceModel.ServiceHost(GetType
    (WcfServiceLibrary1.Service1))  
  4. host.AddServiceEndpoint(GetType(WcfServiceLibrary1.IService1),
     New System.ServiceModel.WSHttpBinding, url)  
  5. host.Open()  
  6. Console.WriteLine(host.State.ToString)  
  7. Dim f As New System.ServiceModel.ChannelFactory
    (Of WcfServiceLibrary1.IService1)(New System.ServiceModel
    .WSHttpBinding, url)  
  8. Dim s As WcfServiceLibrary1.IService1 = f.CreateChannel  
  9. Console.WriteLine(s.GetData(1))  
  10. Console.WriteLine(s.GetData(1))  
  11. Console.WriteLine(s.GetData(1))  
  12. Console.ReadKey()  
  13. End Sub 

 

 

輸出:

 

urn:uuid:e801a8b5-8419-4ec4-bfc7-a850f408a42a

urn:uuid:e801a8b5-8419-4ec4-bfc7-a850f408a42a

urn:uuid:e801a8b5-8419-4ec4-bfc7-a850f408a42a

3. 如果有必要,可以使用 OperationContractAttribute 的 IsInitiating 和 IsTerminating 屬性來控制每次調(diào)用對WCF會話狀態(tài)的操控。

IsInitiating 表示該方法是否可以初始化 Session,IsTerminating 表示該方法是否可以終止 Session。默認(rèn)設(shè)置 IsInitiating=true,IsTerminating=false。

我們將上面的例子改一下:

 

 

  1. < ServiceContract(SessionModeSessionMode:=SessionMode.Required)> _  
  2. Public Interface IService1  
  3. < OperationContract(isinitiating:=False)> _  
  4. Function GetData(ByVal value As Integer) As String  
  5. End Interface 

 

 

 

 

 

客戶第一次調(diào)用時會出現(xiàn)異常

ContractDescription“IService1”沒有 IsInitiating=true 操作;協(xié)定必須至少有一個 IsInitiating=true 操作。

我們可以增加一個額外的方法來初始化會話,如下。OK,這次沒問題了。

  1. < ServiceContract(SessionModeSessionMode:=SessionMode.Required)> _  
  2. Public Interface IService1  
  3. < OperationContract(isinitiating:=False)> _  
  4. Function GetData(ByVal value As Integer) As String  
  5. < OperationContract(isinitiating:=True)> _  
  6. Sub Init()  
  7. End Interface  
  8. < ServiceBehavior(InstanceContextModeInstanceContextMode
    :
    =InstanceContextMode.PerSession)> _  
  9. Public Class Service1  
  10. Implements IService1  
  11. Public Function GetData(ByVal value As Integer) 
    As String Implements IService1.GetData  
  12. Console.WriteLine(OperationContext.Current.SessionId)  
  13. Debug.WriteLine(OperationContext.Current.SessionId)  
  14. Return String.Format("You entered: {0}", value)  
  15. End Function  
  16. Public Sub Init() Implements IService1.Init  
  17. End Sub  
  18. End Class 

客戶端

 

 

  1. Sub Main()  
  2. Dim url As String = "http://localhost:8731/Design_Time_Addresses
    /WcfServiceLibrary1/Service1/mex"
     
  3. Dim host As New System.ServiceModel.ServiceHost(GetType
    (WcfServiceLibrary1.Service1))  
  4. host.AddServiceEndpoint(GetType(WcfServiceLibrary1.IService1),
     New System.ServiceModel.WSHttpBinding, url)  
  5. host.Open()  
  6. Console.WriteLine(host.State.ToString)  
  7. Dim f As New System.ServiceModel.ChannelFactory
    (Of WcfServiceLibrary1.IService1)(New System.ServiceModel.
    WSHttpBinding, url)  
  8. Dim s As WcfServiceLibrary1.IService1 = f.CreateChannel  
  9. s.Init() '初始化  
  10. Console.WriteLine(s.GetData(1))  
  11. Console.WriteLine(s.GetData(1))  
  12. Console.WriteLine(s.GetData(1))  
  13. Console.ReadKey()  
  14. End Sub 

 

 

這個示例就到這里了,對于原文有一個地方:

我們將上面例子改一下。

  1. [ServiceContract(SessionModeSessionMode=SessionMode.Required)]  
  2. public interface ICalculate  
  3. {  
  4. [OperationContract(IsTerminating=true)]  
  5. int Add(int a, int b);  

 

在客戶端第二次調(diào)用 Add 方法時會拋出異常。

未處理 System.InvalidOperationException
Message="This channel cannot send any more messages because IsTerminating operation 'Add' has already been called."
Source="mscorlib"
StackTrace:
Server stack trace:
在 System.ServiceModel.Channels.ServiceChannel.PrepareCall(ProxyOperationRuntime operation, Boolean oneway, ProxyRpc& rpc)

我在測試的過程,沒有發(fā)現(xiàn)這個問題!我用的是vs2008+sp1

以上就是對WCF會話狀態(tài)的相關(guān)介紹。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-02-23 15:58:57

WCF Session

2009-12-21 17:40:25

WCF會話

2010-08-03 12:53:51

FlexBuilder

2010-02-23 09:51:32

WCF MTOM

2009-12-29 16:36:47

Silverlight

2009-12-31 16:50:02

Silverlight

2010-08-09 10:03:43

FlexBuilder

2009-09-04 18:00:54

C# Main方法

2011-11-18 15:01:32

筆記本評測

2015-05-11 13:20:18

云智慧透視寶

2010-02-22 13:35:03

WCF異常處理

2010-02-22 15:00:02

WCF信道工廠

2010-02-23 10:15:22

WCF運行機制

2010-02-23 09:34:15

WCF重載

2010-02-25 14:26:48

WCF特點

2009-11-05 16:12:09

WCF會話服務(wù)

2011-06-03 12:41:36

2010-09-07 15:56:37

PPPoE會話

2010-03-02 17:43:31

WCF框架處理流程

2009-12-10 13:43:08

使用PHPExcel
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 黄网站涩免费蜜桃网站 | 国产精品一区二区不卡 | 午夜精品久久久久久久久久久久久 | 国产成人网 | 亚洲人成人网 | 成人在线免费视频 | sese视频在线观看 | 国产日韩一区二区三区 | 日韩视频在线免费观看 | 精品一区二区三区不卡 | 久久伊人一区二区 | 久久综合狠狠综合久久综合88 | 国内自拍偷拍一区 | 日韩色在线 | 久草久草久草 | 久久久国产一区二区三区 | 在线看av网址 | 国产最新视频在线 | 九色在线观看 | 日本欧美在线观看视频 | 日韩精品一区在线 | 国产一区二区电影 | 精品欧美一区二区三区 | 国产高清自拍视频在线观看 | 精品一区二区三区不卡 | 色网站在线 | 91玖玖 | 四虎影视1304t | 亚洲成人精品 | 久久精品亚洲成在人线av网址 | 精品欧美一区二区在线观看视频 | 特级做a爱片免费69 精品国产鲁一鲁一区二区张丽 | 欧美福利| 欧美一级特黄aaa大片在线观看 | 一区二区三区国产视频 | 97视频网站 | 国产精品国产a | 午夜在线影院 | 在线成人| 男女免费网站 | 国产精品99 |