Problem with manually created SessionState provider

I am working on a prototype program that should test different providers (SessionState, Profile, etc.) for ASP.NET, i.e. MySQL, Oracle, etc. I am currently using the MySQL provider. I just managed to create an instance of the provider, SessionStateContainer and SessionState itself.

Type mySqlType = Type.GetType("MySql.Web.SessionState.MySqlSessionStateStore, MySql.Web, Version=6.6.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d", true, true); SessionStateStoreProviderBase provider = (SessionStateStoreProviderBase)Activator.CreateInstance(mySqlType); System.Collections.Specialized.NameValueCollection providerConfig = new System.Collections.Specialized.NameValueCollection(); providerConfig.Add("connectionStringName", "LocalMySqlServer"); providerConfig.Add("applicationName", "/"); providerConfig.Add("autogenerateschema", "True"); providerConfig.Add("enableExpireCallback", "False"); provider.Initialize("MySqlSessionStateProvider", providerConfig); HttpContext context = new HttpContext( new HttpRequest("fake", "http://localhost:14359", ""), new HttpResponse(new StringWriter())); SessionStateStoreData storeData = provider.CreateNewStoreData(context, 100); System.Web.SessionState.HttpSessionStateContainer sessionStateContainer = new System.Web.SessionState.HttpSessionStateContainer( "mySession", storeData.Items, storeData.StaticObjects, 100, true, System.Web.HttpCookieMode.UseDeviceProfile, SessionStateMode.Custom, false ); Type sessionStateType = Type.GetType("System.Web.SessionState.HttpSessionState, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); HttpSessionState sessionState = (HttpSessionState)sessionStateType .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(IHttpSessionState) }, null) .Invoke(new object[] { ssessionStateContainer }); provider.InitializeRequest(context); 

Everything seems to work fine, as I can write some values ​​in the session that I created, and I can read them from there:

 sessionStateContainer.Add("SomeKey", "SomeValue"); var test = sessionStateContainer["SomeKey"]; Console.WriteLine("var test: " + test.ToString()); // outputs "var test: SomeValue" sessionState.Add("AnotherKey", "SomeOtherValue"); var test2 = sessionState["AnotherKey"]; Console.WriteLine("var test2: " + test2.ToString()); // outputs "var test2: SomeOtherValue" // End the request provider.EndRequest(context); 

Here is the interesting part - when I check the my_aspnet_sessions table in the database, there is nothing there.

So I wonder where the data was written, or am I doing something wrong?

Update:
Despite the fact that this problem is no longer a blocker, I am still interested to find out, and if someone tries to try, here I used connectionString :

 <connectionStrings> <remove name="LocalMySqlServer" /> <add name="LocalMySqlServer" connectionString="server=localhost;database=session_test;User Id=user;password=pass" providerName="MySql.Data.MySqlClient" /> </connectionStrings> 

You will also need the MySQL Connector NET .

+7
source share
2 answers

You do not see the data in the database because the SessionStateModule expects the end of the request to call the SetAndReleaseItemExclusive command. Data is stored in memory until such a command is called. In fact, the documentation states:

The SessionStateModule object calls the SetAndReleaseItemExclusive method at the end of the request during the ReleaseRequestState event to insert current session item information in the data store or update existing session item information in the data store with current values ​​to update the item expiration time and release the data lock. Only the session information for the current application is updated that matches the specified session ID and lockId. For more information about locking, see the "Blocking Session Storage Data" section in the SessionStateStoreProviderBase class overview.

To dwell on the details, you can take a look at the relevant files from the monocode> .

Given this, in order to see your session data in the database, you should do something like:

 object storeLockId = new object(); sessionStateContainer.Add("SomeKey", "SomeValue"); var test = sessionStateContainer["SomeKey"]; Console.WriteLine("var test: " + test.ToString()); sessionState.Add("AnotherKey", "SomeOtherValue"); var test2 = sessionState["AnotherKey"]; Console.WriteLine("var test2: " + test2.ToString()); // End the request provider.SetAndReleaseItemExclusive (context, sessionStateContainer.SessionID, storeData, storeLockId, true); provider.EndRequest(context); 
+3
source

In your web.config you need to set mode = "custom"

  <sessionState mode="Custom" customProvider="YourProvider"> 

Otherwise, session data will be stored in memory regardless of provider.

0
source

All Articles