2010-01-05 67 views
4

我想設置一個使用NetTcpBinding的WcfService。我使用傳輸模式流式傳輸,因爲我將傳輸大型文件。我需要使用會話,我已閱讀,NetTcpBinding的支持。但是,當我打開它,如:NetTcpBinding與流和會話

SessionMode=SessionMode.Required 

我得到的錯誤:

System.InvalidOperationException:合同需要會議,但綁定「 NetTcpBinding'不支持它或沒有正確配置以支持它。

有誰知道我必須做什麼才能使NetTcpBinding與會話一起工作嗎? 感謝您的幫助:)

回答

11

你已經毫無疑問地解決了這個 - 但對於其他人遇到它(像我一樣)......

根據「規劃WCF服務」,由朱瓦爾·洛 - 你不能使用已配置SessionMode.Required的合同使用流式傳輸。請參閱第243頁

既不可以使用NetTcpBinding,也不需要可靠的流式傳輸。

它沒有詳細說明原因。

一個解決辦法可能是分裂需要會話模式劃分成獨立的合同和流式操作到另一個操作。 然後實現其在非流接口作爲RegisterSession(GUID mySessionId)操作通過用於每個客戶端的唯一ID(客戶端應用程序的生命週期獨特GUID)。 當在服務器上創建會話時 - 他們可以使用會話管理器對象進行註冊,該對象將GUID,SessionContractImplemenation對存儲在Dictionary中。

然後設置了一個param添加到流承包經營(相同GUID),這樣流合同執行可以(通過創建會話管理器 - 使用提供的GUID)訪問Live非流對象。

當然,您必須適當地管理會話生存期。

從微軟...

Sessions and Streaming When you have a large amount of data to transfer, the streaming transfer mode in WCF is a feasible alternative to the default behavior of buffering and processing messages in memory in their entirety. You may get unexpected behavior when streaming calls with a session-based binding. All streaming calls are made through a single channel (the datagram channel) that does not support sessions even if the binding being used is configured to use sessions. If multiple clients make streaming calls to the same service object over a session-based binding, and the service object's concurrency mode is set to single and its instance context mode is set to PerSession, all calls must go through the datagram channel and so only one call is processed at a time. One or more clients may then time out. You can work around this issue by either setting the service object's InstanceContextMode to PerCall or Concurrency to multiple.

注:
MaxConcurrentSessions在這種情況下沒有影響,只有一個 「會話」 可用,因爲。

請參閱http://msdn.microsoft.com/en-us/library/ms733040.aspx