-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ISessionState against ISession (#21)
- Loading branch information
1 parent
96b0a9a
commit 49ea3bd
Showing
32 changed files
with
955 additions
and
65 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Session State | ||
|
||
Session state in ASP.NET Framework provided a number of features that ASP.NET Core does not provide. In order to migrate from ASP.NET Framework to Core, the adapters provide mechanisms to enable populating session state with similar behavior as `System.Web` did. Some of the differences between framework and core are: | ||
|
||
- ASP.NET Framework would lock session usage within a session, so subsequent requests in a session are handled in a serial fashion. This is different than ASP.NET Core that does not provide any of these guarantees. | ||
- ASP.NET Framework would serialize and deserialize objects automatically (unless being done in-memory). ASP.NET Core simply provides a mechanism to store a `byte[]` given a key. Any object serialization/deserialization has to be done manually be the user. | ||
|
||
The adapter infrastructure exposes two interfaces that can be used to implement any session storage system. These are: | ||
|
||
- `Microsoft.AspNetCore.SystemWebAdapters.ISessionManager`: This has a single method that gets passed an `HttpContext` and the session metadata and expects an `ISessionState` object to be returned. | ||
- `Microsoft.AspNetCore.SystemWebAdapters.ISessionState`: This describes the state of a session object. It is used as the backing of the `System.Web.SessionState.HttpSessionState` type. | ||
|
||
## Serialization | ||
Since the adapters provide the ability to work with strongly-typed session state, we must be able to serialize and deserialize types. This is accomplished through implementation of the type `Microsoft.AspnetCore.SysteWebAdapters.SessionState.Serialization.ISessionSerializer`, of which a JSON implementation is provided. | ||
|
||
Serialization and deserialization of session keys requires additional information which is configured via the `SessionSerializerOptions`: | ||
|
||
- `RegisterKey<T>(string)` - Registers a session key to a known type. This is required in order to serialize/deserialize the session state correctly. If a key is found that there is no registration for, an error will be thrown and session will not be available. | ||
|
||
To use the default JSON backed implementation, add the following to the startup: | ||
|
||
```csharp | ||
builder.Services.AddSystemWebAdapters() | ||
.AddJsonSessionSerializer(options => | ||
{ | ||
options.RegisterKey<int>("test-value"); | ||
}); | ||
``` | ||
|
||
## Implementations | ||
|
||
There are two available implementations of the session state object that currently ship, each with some trade offs of features. The best choice for an application may depend on which part of the migration it is in, and may change over time. | ||
|
||
- Strongly typed: Provides the ability to access an object and can be cast to the expected type | ||
- Locking: Ensures multiple requests within a single session are queued up and aren't accessing the session at the same time | ||
- Standalone: Can be used when there is just a .NET Core app without needing additional support. | ||
|
||
Below are the available implementations: | ||
|
||
| Implementation | Strongly typed | Locking | Standalone | | ||
|-------------------------------------------------------------|----------------|---------|------------| | ||
| [Remote app](remote-session.md) | ✔️ | ✔️ | ⛔ | | ||
| [Wrapped ASP.NET Core](wrapped-aspnetcore-session.md) | ✔️ | ⛔ | ✔️ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Wrapped ASP.NET Core Session State | ||
|
||
This implementation wraps the session provided on ASP.NET Core so that it can be used with the adapters. The session will be using the same backing store as `Microsoft.AspNetCore.Http.ISession` but will provide strongly-typed access to its members. | ||
|
||
Configuration for ASP.NET Core would look similar to the following: | ||
|
||
```csharp | ||
builder.Services.AddSystemWebAdapters() | ||
.AddJsonSessionSerializer(options => | ||
{ | ||
options.RegisterKey<int>("test-value"); | ||
options.RegisterKey<SessionDemoModel>("SampleSessionItem"); | ||
}) | ||
.WrapAspNetCoreSession(); | ||
``` | ||
|
||
The framework app would not need any changes to enable this behavior. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
src/Microsoft.AspNetCore.SystemWebAdapters.SessionState/Serialization/ISessionSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
|
||
namespace Microsoft.AspNetCore.SystemWebAdapters.SessionState.Serialization; | ||
|
||
public interface ISessionSerializer | ||
{ | ||
ISessionState? Deserialize(string? data); | ||
ISessionState? Deserialize(string? input); | ||
|
||
byte[] Serialize(ISessionState state); | ||
|
||
byte[] Serialize(string key, object value); | ||
|
||
object? Deserialize(string key, Memory<byte> bytes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.