環境所逼,在雲端上用Session的方法[Windows Azure]
在雲端環境中,In-Process Session是可以正常的運作,但因為Loading Balance的關係,假如你設了兩個Instance以上,
這個方法可不管用,因此這裡的解法,就是將Session改存在雲端的儲存體上,這樣不管幾個Instance,都可以共同存取同樣的Session。
以下說明步驟:
1.在Web.config中加入以下程式碼
<system.web>
<sessionState mode="Custom" customProvider="TableStorageSessionStateProvider">
<providers>
<clear/>
<add name="TableStorageSessionStateProvider" type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider" />
</providers>
</sessionState>
--other code--
</system.web>
2.去這裡下載Windows Azure Platform Training Kit,將裡面的 AspProviders.dll 加入參考
(注意:AspProviders是用DataConnectionString來與Storage連結,我是先改為DiagnosticsConnectionString,compile過DLL後才加入參考,因為我很懶= =)
3.到此步驟就可以正常使用Session了,寫法上不變,原理就是將Session存入Table及Blob中。