由於害怕提出一個已經飽和的Stack Overflow問題,我一直在滾動瀏覽可能的解決方案,而且沒有更聰明的辦法來實現我想實現的目標,如果這確實是可能的,或者我是否設計錯誤。配置WCF端點以啓用WebScript和會話
這個問題不僅僅是尋找解決方案,而是要解釋一些WCF在配置/定製不同端點時的複雜性(以及誤解)。
的典型場景:基於網絡的結算系統中,客戶可以通過使JavaScript調用回服務器改變屬性,如數量或他們的籃內的產品的尺寸。服務器必須知道用戶是誰,因此有可用會話似乎是理想的解決方案。
WCF絕對看起來很不錯,因爲這種情況可以在未來的日期擴展以支持另一個終端,比如移動應用或其他服務。 (在我的問題範圍之外,但驗證我想使用WCF來處理遺留的.Net Web服務)
上面的問題是配置端點/綁定。
- 的WebHttpBinding - 這是用來使網頁腳本可以訪問服務(即Java腳本從網頁)。但它不支持會話
- wsHttpBinding - 這支持會話,但不支持web腳本。
我玩過各種配置。看起來我需要上述綁定的組合,或者可以創建支持這些元素的自定義綁定?如果是這樣,有沒有什麼好的資源如何做到這一點?我嘗試創建一個自定義綁定,並失敗慘痛!
我讀過關於其他問題的各種意見,建議您不要通過Web腳本使用Sessions,WCF不支持它,或者正在實施的系統設計不正確。首先,WebServices支持這一點,所以我很難相信WCF不會,尤其是它支持Web腳本和會話(但不是在一起?可能......)。如果我要使用除會話以外的其他內容,那麼它必須是基於令牌的系統,以便可以識別用戶,但肯定這實際上是一個會話。我可以建立這個,但它似乎重新創建輪子。
這是我的Web.Config內的配置。我已經設置了3個端點來玩;一個基本的,一個支持會議和其他支持webscripts。 (該地址的WebHttpBinding端點是空白的,因爲該服務時在調試模式下返回一個錯誤 - 我見過幾個人,也該說明)
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="CustomerServiceAspNetAjaxBehavior">
<enableWebScript />
<!--<webHttp />-->
</behavior>
<behavior name="WebScript">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service behaviorConfiguration="serviceBehavior" name="CustomerService">
<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="basicBinding"
contract="CustomerService" />
<endpoint address="ws" binding="wsHttpBinding" bindingConfiguration="wsConfig"
contract="CustomerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="" behaviorConfiguration="CustomerServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webConfig" contract="CustomerService" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="basicBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None"></security>
<readerQuotas maxStringContentLength="2147483647 "/>
</binding>
</basicHttpBinding>
<wsHttpBinding>
<binding name="wsConfig" transactionFlow="true">
<security mode="None" />
<reliableSession enabled="true" ordered="true"/>
</binding>
</wsHttpBinding>
<webHttpBinding>
<binding name="webConfig">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<client/>
</system.serviceModel>
,這是我的服務接口,可見我有將SessionMode設置爲Allow,GetSession()返回當前會話ID;如果會話不可用,則返回null。
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
[ServiceContract(Namespace = "", ConfigurationName = "CustomerService", SessionMode = SessionMode.Allowed)]
public interface ICustomerService
{
[OperationContract()]
[WebGet()]
bool UpdateBasketItem(int index, int productId, int qty, int attribSize);
[OperationContract()]
[WebGet()]
string GetSession();
}
所以我的長篇大論的問題是這樣的..我可以配置一個端點啓用webscript,這樣我可以通過JavaScript訪問該服務,並在同一時間啓用會話?我已經通過WCF測試客戶端單獨測試了端點,以查看'ws'端點是否有會話,並且使用webHttpBinding的空白端點沒有會話,但是可以從Javascript調用。
我知道可能沒有「開箱即用」的綁定,所以我需要創建一個自定義綁定,或者我可以通過使用一些配置魔術來變形上述兩個端點嗎?
這確實看起來像我沒有考慮過的可能路線,時間早已成爲我的敵人,所以我可能最終走上了這條路! – Radderz 2014-10-27 22:58:56
對於發現問題出在哪裏,我仍然是個傻瓜;) – Radderz 2014-10-27 23:01:11