[研究][ASP.NET]使用 SQL Server 儲存 Session State 會談狀態(三)Microsoft.AspNet.Providers
2022-04-12
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
續這篇
[研究][ASP.NET]使用 SQL Server 儲存 Session State 會談狀態(一)aspnet_regsql.exe
https://shaurong.blogspot.com/2022/04/aspnet-sql-server-session-state.html
[研究][ASP.NET]使用 SQL Server 儲存 Session State 會談狀態(二)System.Web.Providers
https://shaurong.blogspot.com/2022/04/aspnet-sql-server-session-state_12.html
用aspnet_regsql.exe需要自己手動在SQL Server建立資料表,用 NuGet 安裝 System.Web.Providers可讓系統自動在資料庫建立 dbo.sessions資料表。
先用 NuGet 安裝 System.Web.Providers 1.2.0,其實描述有說 Legacy package, System.Web.Providers is now included in the 'Microsoft.AspNet.Providers' package.,這次不要安裝 System.Web.Providers,直接安裝 Microsoft.AspNet.Providers。
ASP.NET Universal Providers add provider support in ASP.NET 4 for all editions of SQL Server 2005 and later and to SQL Azure. If you use these providers to develop your application, the application will be ready for cloud environments like Azure. Other than supporting additional storage options, the providers work like the existing SQL-based providers, so that you can easily switch an application to use cloud storage via SQL Azure.
它會要求安裝相依套件:
EntityFramework.6.0.0
Microsoft.AspNet.Providers.Core.2.0.0
Microsoft.AspNet.Providers.2.0.0
NuGet會在Web.Config建立一個資料庫連線 DefaultConnection,還有一堆設定使用該連線。
之後隨便建立一個 Default.aspx (隨便取個名稱),拖拉一個SqlDataSource控制項過去,設定資料庫連線,假設稱為 TestDB7ConnectionString。(要先在 TestDB7 中隨便建立一個資料表才能做)
最後把 Web.Config 中 "DefaultConnection" 字串全換成 "TestDB6ConnectionString",把原來的 資料庫連線 DefaultConnection 註解掉。再把 sessionState mode="InProc" 換成 sessionState mode="Custom",結果如下:
Web.Config
<?xml version="1.0" encoding="utf-8"?> <!-- 如需如何設定 ASP.NET 應用程式的詳細資訊,請前往 https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <system.web> <compilation debug="true" targetFramework="4.8" /> <httpRuntime targetFramework="4.8" /> <profile defaultProvider="DefaultProfileProvider"> <providers> <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="TestDB7ConnectionString" applicationName="/" /> </providers> </profile> <membership defaultProvider="DefaultMembershipProvider"> <providers> <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="TestDB7ConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <roleManager defaultProvider="DefaultRoleProvider"> <providers> <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="TestDB7ConnectionString" applicationName="/" /> </providers> </roleManager> <!-- If you are deploying to a cloud environment that has multiple web server instances, you should change session state mode from "InProc" to "Custom". In addition, change the connection string named "TestDB7ConnectionString" to connect to an instance of SQL Server (including SQL Azure and SQL Compact) instead of to SQL Server Express. --> <sessionState mode="Custom" customProvider="DefaultSessionProvider"> <providers> <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="TestDB7ConnectionString" /> </providers> </sessionState> </system.web> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v15.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> <connectionStrings> <!--<add name="TestDB7ConnectionString" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=aspnet-WebApplication1-20220412142205;Integrated Security=SSPI" providerName="System.Data.SqlClient" />--> <add name="TestDB7ConnectionString" connectionString="Data Source=.;Initial Catalog=TestDB7;Persist Security Info=True;User ID=sa;Password=P@ssw0rd" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> |
在 Visual Studio 2022 中 Default.aspx 按下 F5 執行,TestDB7資料庫中會自動建立 dbo.sessions 資料表。
不過說實話,它在 Web.Config 加入太多東西,敝人偏好第一種方式自己手動用 aspnet_regsql.exe 建立,除非你沒有 DB Server 的 Windows 作業系統權限,無法使用第一種方式,就只能用這種。
(完)
相關
[研究][ASP.NET]使用 SQL Server 儲存 Session State 會談狀態(一)aspnet_regsql.exe
https://shaurong.blogspot.com/2022/04/aspnet-sql-server-session-state.html
[研究][ASP.NET]使用 SQL Server 儲存 Session State 會談狀態(二)System.Web.Providers
https://shaurong.blogspot.com/2022/04/aspnet-sql-server-session-state_12.html
[研究][ASP.NET]使用 SQL Server 儲存 Session State 會談狀態(三)Microsoft.AspNet.Providers
https://shaurong.blogspot.com/2022/04/aspnet-sql-server-session-state_28.html
沒有留言:
張貼留言