首先,我承認我是WCF的新手。仍然沒有脫離訓練輪。我需要開發一個只能通過HTTP/HTTPS公開的WCF服務。如何使用HTTP cookie?
我被分配去開發一個WCF服務,其中一部分需求是每個請求都需要傳遞一種「會話令牌」作爲HTTP cookie。 (可以預料的是,這種令牌需要在此類服務中成功「登錄」呼叫的HTTP響應頭中生成)。
這是直截了當的嗎?
首先,我承認我是WCF的新手。仍然沒有脫離訓練輪。我需要開發一個只能通過HTTP/HTTPS公開的WCF服務。如何使用HTTP cookie?
我被分配去開發一個WCF服務,其中一部分需求是每個請求都需要傳遞一種「會話令牌」作爲HTTP cookie。 (可以預料的是,這種令牌需要在此類服務中成功「登錄」呼叫的HTTP響應頭中生成)。
這是直截了當的嗎?
聲明:你不是真的應該這樣做,因爲它迫使WCF服務表現爲Web服務。但如果你需要cookies,請繼續閱讀。
如果你需要的是會話ID,就可以得到它:
OperationContext.Current.SessionId
如果你需要的cookie,你需要通過一些跳鐵圈。它的要點是(1)設置asp.net兼容性,以及(2)引用HttpContext.Current屬性。
您的服務需要使用wsHttpBinding(或支持會話的其他綁定)。如果將您的項目創建爲IIS中託管的WCF服務,則默認情況下會獲得這些服務。您還需要在配置文件中設置asp.net兼容性。
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<bindings>
<wsHttpBinding>
<binding name="MyBinding" allowCookies="false" ... </binding>
</wsHttpBinding>
</bindings>
(見爲什麼我allowCookies鏈接here = FALSE)
要啓用會話,你的WCF服務合同,設置以下
[ServiceContract(SessionMode=SessionMode.Required)]
public interface IMyWcfService {...}
您可能還需要在服務本身上設置ServiceBehavior(PerSession是默認設置),並且您需要設置asp.net兼容性。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
public class MyWcfService : IMyWcfService {...}
一些相關的屬性,那麼你必須訪問:
// Gives you the current session id as a string
HttpContext.Current.Session.SessionID
// Indicates whether the service is using sessionless cookies
HttpContext.Current.Session.CookieMode
// Indicates whether the session id is stored in the url or in an HTTP cookie
HttpContext.Current.Session.IsCookieless
// The cookies themselves
HttpContext.Current.Request.Cookies
HttpContext.Current.Response.Cookies
// The session and cache objects
HttpContext.Current.Cache
HttpContext.Current.Session
在WCF服務上的會話的鏈接:
http://msdn.microsoft.com/en-us/library/ms733040.aspx
HTH,
詹姆斯
MSDN上的這個主題可以幫助您解決問題http://msdn.microsoft.com/en-us/library/bb398778.aspx。
而且,這可以幫助您與內部託管you`r WCF服務IIS:http://msdn.microsoft.com/en-us/library/aa702682.aspx 和http://msdn.microsoft.com/en-us/library/bb332338.aspx(主機使用Internet Information Services)
這只是回答您的問題部分,但會給你一個良好的開端與配置。 在根據服務配置部分你的配置文件,創建基本的HTTP綁定這樣的:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="myHttpBinding" allowCookies="true">
</binding>
</basicHttpBinding>
</system.serviceModel>
然後在WCF綁定和端點配置讀取了。
沒有做過那些答案幫助你? – 2010-12-03 20:27:16
對不起,需要回答已被棄用。要求已經改變,現在我可以在對象中傳遞東西。我應該怎樣處理這個問題,刪除它? – JCCyC 2010-12-08 18:19:56