Skip to content

Commit 6cb428c

Browse files
committed
more session
1 parent 5597f87 commit 6cb428c

3 files changed

Lines changed: 49 additions & 17 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Enable session
2+
3+
Session support requires explicit activation. Configure it per-route using ASP.NET Core metadata.
4+
5+
#### Option 1: Annotate controllers
6+
7+
:::code language="csharp" source="~/migration/fx-to-core/areas/session/samples/remote/SomeController.cs" id="snippet_Controller" :::
8+
9+
#### Option 2: Enable globally for all endpoints
10+
11+
:::code language="csharp" source="~/migration/fx-to-core/areas/session/samples/remote/Program.cs" id="snippet_RequireSystemWebAdapterSession" :::

aspnetcore/migration/fx-to-core/areas/session.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ Your Framework application requires no changes.
133133

134134
For more information, see the [wrapped session state sample app](https://github.com/dotnet/systemweb-adapters/blob/main/samples/SessionLocal/SessionLocalCore/Program.cs)
135135

136+
[!INCLUDE[](~/migration/fx-to-core/areas/includes/enable-session.md)]
137+
136138
## Remote app session state
137139

138140
[!INCLUDE[](~/migration/fx-to-core/includes/uses-systemweb-adapters.md)]
@@ -149,18 +151,22 @@ Complete the [remote app setup](xref:migration/fx-to-core/inc/remote-app-setup)
149151

150152
The <xref:System.Web.SessionState.HttpSessionState> object requires serialization for remote app session state.
151153

152-
In order to serialize session state, a serializer for the state object must be registered:
153-
154-
:::code language="csharp" source="~/migration/fx-to-core/areas/session/samples/remote/Program.cs" id="snippet_Serialization" :::
155-
156-
In ASP.NET Core, [BinaryFormatter](/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter) was used to automatically serialize session value contents. In order to serialize these with for use with the System.Web adapters, the serialization must be explicitly configured using `ISessionKeySerializer` implementations.
154+
In ASP.NET Framework, [BinaryFormatter](/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter) was used to automatically serialize session value contents. In order to serialize these with for use with the System.Web adapters, the serialization must be explicitly configured using `ISessionKeySerializer` implementations.
157155

158156
Out of the box, there is a simple JSON serializer that allows each session key to be registered to a known type using `JsonSessionSerializerOptions`:
159157

160158
* `RegisterKey<T>(string)` - Registers a session key to a known type. This registration is required for correct serialization/deserialization. Missing registrations cause errors and prevent session access.
161159

162160
:::code language="csharp" source="~/migration/fx-to-core/areas/session/samples/serialization/Program.cs" id="snippet_Serialization" :::
163161

162+
If more customization is needed, then `ISessionKeySerializer` can be implemented:
163+
164+
:::code language="csharp" source="~/migration/fx-to-core/areas/session/samples/serialization/Program_Custom.cs" id="snippet_Serialization" :::
165+
:::code language="csharp" source="~/migration/fx-to-core/areas/session/samples/serialization/Program_Custom.cs" id="snippet_CustomSerializer" :::
166+
167+
> [!NOTE]
168+
> When using the `AddJsonSessionSerializer` registration pattern, there is no need to call `AddSessionSerializer` as it will automatically be added. If you only want to use a customimplementation, then you must manually add it.
169+
164170
### Application configuration
165171

166172
:::zone pivot="manual"
@@ -194,18 +200,7 @@ var coreApp = builder.AddProject<Projects.CoreApplication>("core")
194200

195201
:::zone-end
196202

197-
### Enable session
198-
199-
Session support requires explicit activation. Configure it per-route using ASP.NET Core metadata.
200-
201-
#### Option 1: Annotate controllers
202-
203-
:::code language="csharp" source="~/migration/fx-to-core/areas/session/samples/remote/SomeController.cs" id="snippet_Controller" :::
204-
205-
#### Option 2: Enable globally for all endpoints
206-
207-
:::code language="csharp" source="~/migration/fx-to-core/areas/session/samples/remote/Program.cs" id="snippet_RequireSystemWebAdapterSession" :::
208-
203+
[!INCLUDE[](~/migration/fx-to-core/areas/includes/enable-session.md)]
209204

210205
### Communication protocol
211206

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// <snippet_Serialization>
4+
builder.Services.AddSystemWebAdapters()
5+
.AddSessionSerializer();
6+
7+
builder.Services.AddSingleton<ISessionKeySerializer, CustomSessionKeySerializer>();
8+
// </snippet_Serialization>
9+
10+
var app = builder.Build();
11+
app.Run();
12+
13+
// <snippet_CustomSerializer>
14+
sealed class CustomSessionKeySerializer : ISessionKeySerializer
15+
{
16+
public bool TryDeserialize(string key, byte[] bytes, out object? obj)
17+
{
18+
// Custom deserialization logic
19+
}
20+
21+
public bool TrySerialize(string key, object? value, out byte[] bytes)
22+
{
23+
// Custom serialization logic
24+
}
25+
}
26+
// </snippet_CustomSerializer>

0 commit comments

Comments
 (0)