.Net 站點使用Redis存儲Session信息,借助Redis實現多個站點公用Session信息
1.新建一個web項目,我新建的是一個空的MVC的項目。
新建好之后添加Home控制器,新建Index Action方法和View視圖文件。
2.使用NuGet添加RedisSessionStateProvider這個引用。
安裝完成之后會自動添加三個引用:
Microsoft.Web.RedisSessionStateProvider
StackExchange.Redis.StrongName
StackExchange.Redis
Web.config 會自動添加如下的配置文件
<sessionState mode="Custom" customProvider="MySessionStateStore"> <providers> <!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki --> <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. --> <!-- 'throwOnError','retryTimeoutInMilliseconds','databaseId' and 'applicationName' can be used with both options. --> <!-- <add name="MySessionStateStore" host = "127.0.0.1" [String] port = "" [number] accessKey = "" [String] ssl = "false" [true|false] throwOnError = "true" [true|false] retryTimeoutInMilliseconds = "5000" [number] databaseId = "0" [number] applicationName = "" [String] connectionTimeoutInMilliseconds = "5000" [number] operationTimeoutInMilliseconds = "1000" [number] connectionString = "<Valid StackExchange.Redis connection string>" [String] settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String] settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of 'String', which is basically 'connectionString' value.>" [String] loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String] loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String] /> --> <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="" accessKey="" ssl="true" /> </providers> </sessionState>
host改為你的redis服務所在的ip地址,我的是127.0.0.1,ssl給為false,端口號如果不是默認的端口,就自行修改port="XXXX"。
3.在Index方法中添加Session的測試代碼測試Redis保存數據是否成功。如果保存的是復雜類型,要加上可序列化標簽[Serializable],如下是我的測試代碼
public class HomeController : Controller { private static string _webSiteStr; public static string WebSite { get { if (string.IsNullOrWhiteSpace(_webSiteStr)) { _webSiteStr = System.Configuration.ConfigurationManager.AppSettings["WebSite"]; } return _webSiteStr; } } // GET: Home public ActionResult Index() { System.Web.HttpContext.Current.Session["firstTest"] = "1231"; Student p = new Student() { Age=26,Name="liuyu7177" }; System.Web.HttpContext.Current.Session["Student"] = p; return View(); } // GET: Home public ContentResult Two() { var str = (string)System.Web.HttpContext.Current.Session["firstTest"] + WebSite; var p = (Student)System.Web.HttpContext.Current.Session["Student"]; return Content(str); } } [Serializable] public class Student { public string Name { get; set; } public int Age { get; set; } }
其中WebSite這個靜態屬性是在發布多個站點時用來區分訪問到的是那個站點。
在Web.config 中添加一個節點 <add key="WebSite" value="WebSite1" />
啟動調試,如果/Home/Two 頁面成功返回Session里面的內容,說明借助Redis存儲Session數據成功。
4.發布兩個或兩個以上的站點
用剛剛新建的項目在IIS上掛兩個站點。新建兩個發布目錄,更改Web.config文件,一個value設置WebSite1,一個value設置WebSite2.
第一個站點命名為RedisSessionTest,綁定的ip為127.0.0.1 端口是88
第二個站點命名為RedisSessionTestTwo,綁定的ip為127.0.0.1 端口是89
打開瀏覽器分別訪問:http://127.0.0.1:88/Home/Index,http://127.0.0.1:89/Home/Two,http://127.0.0.1:88/Home/Two
第一個站點返回1231WebSite1,第二個站點返回1231WebSite2,說明Session共享成功。
有兩個問題要注意:
1.這個兩個站點是在同樣的頂級域名之下。
2.第二個站點不要打開Home/Index頁面,不然就不能確定第二個站點是否和第一個站點共享Session了。
最后:Redis要安裝比較新的版本,ssl要設置為false,不然可能會報 No connection is available to service this operation: EVAL 這個錯誤
