2013-01-08 65 views
0

我有一個連接到多個客戶端的WCF服務。 服務的配置如下:多線程環境下的OperationContext.Current.SessionId

ServiceBehavior(
     InstanceContextMode = InstanceContextMode.Single, 
     UseSynchronizationContext = false, 
     ... 
在我使用標識使用OperationContext.Current.SessionId客戶服務

public void Register() 
{ 
     Debug.WriteLine(OperationContext.Current.SessionId); 
} 

試想以下情形: - 客戶端1通話記錄 - 運行服務註冊在線程1 - 在一定時間後客戶端2調用註冊 - 運行服務註冊也是線程1(這在理論上是可能的,沒有?)

明知OperationContext.Current是ThreadStatic, 什麼顯示第二個電話:

  • 會議1,因爲OperationContext.Current已經在第一次調用
  • 或者Session 2中設置。

預先感謝您...

回答

0

你會得到工作階段。會話和實例化在WCF中是兩個不同的東西。我建議你閱讀以下2篇博客文章,這將有助於你理解WCF會話和實例的概念。

http://www.dotnetconsult.co.uk/weblog2/PermaLink,guid,af6e6325-2e30-42e3-acb9-57e1363fa51e.aspx

http://www.dotnetconsult.co.uk/weblog2/PermaLink,guid,1efac821-2b89-47bb-9d27-48f0d6347914.aspx

你也可以與客戶端進行測試。我改變了你的WCF方法來返回sessionID並在客戶端使用它。

static void Main(string[] args) 
{ 
     ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(); 
     Console.WriteLine(client.Regster()); 
     Console.ReadLine(); 
     client.Close(); 
} 
+0

感謝您的博客條目,非常有幫助。 –