Pracuję nad programem prototypowym, który powinien przetestować różnych dostawców (SessionState, Profile, itp.) Dla ASP.NET, tj. MySQL, Oracle itp. W tej chwili używam dostawca MySQL. Właśnie udało mi się utworzyć instancję dostawcy, SessionStateContainer i samego SessionState.Problem z ręcznie utworzonym dostawcą SessionState
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);
Wszystko wydaje się działać dobrze, jak jestem w stanie napisać niektóre wartości, w sesji I stworzył, a ja jestem w stanie odczytać je stamtąd:
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);
Nadchodzi interesująca część - kiedy sprawdzam tabelę my_aspnet_sessions
w DB, nic tam nie ma.
Zastanawiam się, gdzie zostały zapisane dane lub czy robię coś nie tak?
Aktualizacja:
Choć ta kwestia nie jest już bloker, wciąż jestem ciekaw, a jeśli ktoś jest gotowy do nadając jej spróbować, oto connectionString
Użyłem:
<connectionStrings>
<remove name="LocalMySqlServer" />
<add name="LocalMySqlServer" connectionString="server=localhost;database=session_test;User Id=user;password=pass" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Będziesz także potrzebować MySQL Connector NET.
można być wyczyszczenie danych w tej linii, tworząc nowy '.Invoke (nowy obiekt [] {} ssessionStateContainer); "co się stanie, gdy przejdziesz przez kod, czy w ogóle widzisz jakieś dane? – MethodMan
Dwa fragmenty są w tej samej kolejności w moim kodzie. Zapis danych kończy się po utworzeniu 'sessionState', więc nie widzę, jak można to wyczyścić? Podczas przechodzenia przez kod widzę klucze 'SomeKey' i' AnotherKey' w odpowiednich właściwościach, ale więcej - nie. – Havelock