Skip to content

Commit 8f8ca9c

Browse files
authored
Separate out JSON session from others so it can be overridden (#84)
1 parent 9cad04d commit 8f8ca9c

File tree

8 files changed

+112
-73
lines changed

8 files changed

+112
-73
lines changed

docs/session-state/remote-session.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
Remote app session state will enable communication between the ASP.NET Core and ASP.NET app and to retrieve the session state. This is enabled by exposing an endpoint on the ASP.NET app that can be queried to retrieve and set the session state.
44

5+
# HttpSessionState serialization
6+
7+
The `System.Web.SessionState.HttpSessionState` object must be serialized for remote app session state to be enabled. This is accomplished through implementation of the type `Microsoft.AspNetCore.SysteWebAdapters.SessionState.Serialization.ISessionSerializer`, of which a default binary writer implementation is provided. This is added by the following code:
8+
9+
```csharp
10+
builder.Services.AddSystemWebAdapters()
11+
.AddSessionSerializer(options =>
12+
{
13+
// Customize session serialization here
14+
});
15+
```
16+
517
## Configuration
618

719
In order to configure it, both the framework and core app must set an API key as well as register known app settings types. These properties are:
@@ -52,7 +64,7 @@ The framework equivalent would look like the following change in `Global.asax.cs
5264

5365
```csharp
5466
Application.AddSystemWebAdapters()
55-
.AddRemoteAppSession(
67+
.AddJsonRemoteAppSession(
5668
// Provide a strong API key that will be used to authenticate the request on the remote app for querying the session
5769
options => options.ApiKey = "strong-api-key",
5870
options =>

docs/session-state/session.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ The adapter infrastructure exposes two interfaces that can be used to implement
1111
- `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.
1212

1313
## Serialization
14-
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.
14+
Since the adapters provide the ability to work with strongly-typed session state, we must be able to serialize and deserialize types. This is customized through the `Microsoft.AspNetCore.SystemWebAdapters.SessionState.Serialization.ISessionKeySerializer`.
1515

16-
Serialization and deserialization of session keys requires additional information which is configured via the `JsonSessionSerializerOptions`:
16+
A default JSON implementation is provided that is configured via the `JsonSessionSerializerOptions`:
1717

18-
- `ThrowOnUnknownSessionKey` - Gets or sets if an unknown key should cause serialization to fail or if it will log a warning and ignore it.
1918
- `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.
2019

2120
To use the default JSON backed implementation, add a package reference to [Microsoft.AspNetCore.SystemWebAdapters.SessionState](https://www.nuget.org/packages/Microsoft.AspNetCore.SystemWebAdapters.SessionState) and add the following to the startup:

samples/MvcApp/Global.asax.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected void Application_Start()
1818

1919
Application.AddSystemWebAdapters()
2020
.AddProxySupport(options => options.UseForwardedHeaders = true)
21-
.AddRemoteAppSession(
21+
.AddJsonRemoteAppSession(
2222
ConfigureRemoteServiceOptions,
2323
options => ClassLibrary.RemoteServiceUtils.RegisterSessionKeys(options.KnownKeys))
2424
.AddRemoteAppAuthentication(o => ConfigureRemoteServiceOptions(o.RemoteServiceOptions));
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using Microsoft.AspNetCore.SystemWebAdapters.SessionState.RemoteSession;
6+
using Microsoft.AspNetCore.SystemWebAdapters.SessionState.Serialization;
7+
8+
namespace Microsoft.AspNetCore.SystemWebAdapters;
9+
10+
public static class JsonRemoteAppSessionStateExtensions
11+
{
12+
public static ISystemWebAdapterBuilder AddJsonRemoteAppSession(this ISystemWebAdapterBuilder builder, Action<RemoteAppSessionStateOptions> configureRemote, Action<JsonSessionSerializerOptions> configureSerializer)
13+
{
14+
if (builder is null)
15+
{
16+
throw new ArgumentNullException(nameof(builder));
17+
}
18+
19+
if (configureRemote is null)
20+
{
21+
throw new ArgumentNullException(nameof(configureRemote));
22+
}
23+
24+
if (configureSerializer is null)
25+
{
26+
throw new ArgumentNullException(nameof(configureSerializer));
27+
}
28+
29+
var serializerOptions = new JsonSessionSerializerOptions();
30+
configureSerializer(serializerOptions);
31+
32+
var keySerializer = new JsonSessionKeySerializer(serializerOptions);
33+
34+
return builder.AddRemoteAppSession(configureRemote, keySerializer);
35+
}
36+
}

src/Microsoft.AspNetCore.SystemWebAdapters.SessionState/RemoteSession/Framework/RemoteAppSessionStateExtensions.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.SystemWebAdapters;
99

1010
public static class RemoteAppSessionStateExtensions
1111
{
12-
public static ISystemWebAdapterBuilder AddRemoteAppSession(this ISystemWebAdapterBuilder builder, Action<RemoteAppSessionStateOptions> configureRemote, Action<JsonSessionSerializerOptions> configureSerializer)
12+
public static ISystemWebAdapterBuilder AddRemoteAppSession(this ISystemWebAdapterBuilder builder, Action<RemoteAppSessionStateOptions> configureRemote, ISessionKeySerializer keySerializer)
1313
{
1414
if (builder is null)
1515
{
@@ -21,22 +21,39 @@ public static ISystemWebAdapterBuilder AddRemoteAppSession(this ISystemWebAdapte
2121
throw new ArgumentNullException(nameof(configureRemote));
2222
}
2323

24-
if (configureSerializer is null)
24+
if (keySerializer is null)
2525
{
26-
throw new ArgumentNullException(nameof(configureSerializer));
26+
throw new ArgumentNullException(nameof(keySerializer));
2727
}
2828

29-
var options = new RemoteAppSessionStateOptions();
30-
configureRemote(options);
31-
3229
// We don't want to throw by default on the .NET Framework side as then the error won't be easily visible in the ASP.NET Core app
33-
var serializerOptions = new JsonSessionSerializerOptions();
34-
configureSerializer(serializerOptions);
30+
var serializerOptions = new SessionSerializerOptions { ThrowOnUnknownSessionKey = false };
31+
var serializer = new BinarySessionSerializer(keySerializer, serializerOptions);
32+
33+
return builder.AddRemoteAppSession(configureRemote, serializer);
34+
}
35+
36+
public static ISystemWebAdapterBuilder AddRemoteAppSession(this ISystemWebAdapterBuilder builder, Action<RemoteAppSessionStateOptions> configureRemote, ISessionSerializer serializer)
37+
{
38+
if (builder is null)
39+
{
40+
throw new ArgumentNullException(nameof(builder));
41+
}
42+
43+
if (configureRemote is null)
44+
{
45+
throw new ArgumentNullException(nameof(configureRemote));
46+
}
47+
48+
if (serializer is null)
49+
{
50+
throw new ArgumentNullException(nameof(serializer));
51+
}
3552

36-
var keySerializer = new JsonSessionKeySerializer(serializerOptions);
37-
var serializer = new BinarySessionSerializer(keySerializer, new SessionSerializerOptions { ThrowOnUnknownSessionKey = false });
53+
var remoteOptions = new RemoteAppSessionStateOptions();
54+
configureRemote(remoteOptions);
3855

39-
builder.Modules.Add(new RemoteSessionModule(options, new InMemoryLockedSessions(serializer), serializer));
56+
builder.Modules.Add(new RemoteSessionModule(remoteOptions, new InMemoryLockedSessions(serializer), serializer));
4057

4158
return builder;
4259
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using Microsoft.AspNetCore.SystemWebAdapters.SessionState.Serialization;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.DependencyInjection.Extensions;
8+
9+
namespace Microsoft.AspNetCore.SystemWebAdapters;
10+
11+
public static class JsonSessionSerializerExtensions
12+
{
13+
public static ISystemWebAdapterBuilder AddJsonSessionSerializer(this ISystemWebAdapterBuilder builder, Action<JsonSessionSerializerOptions>? configure = null)
14+
{
15+
if (builder is null)
16+
{
17+
throw new ArgumentNullException(nameof(builder));
18+
}
19+
20+
builder.AddSessionSerializer();
21+
builder.Services.TryAddSingleton<ISessionKeySerializer, JsonSessionKeySerializer>();
22+
23+
var options = builder.Services.AddOptions<JsonSessionSerializerOptions>();
24+
25+
if (configure is not null)
26+
{
27+
options.Configure(configure);
28+
}
29+
30+
return builder;
31+
}
32+
}

src/Microsoft.AspNetCore.SystemWebAdapters.SessionState/Serialization/SessionSerializerExtensions.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,4 @@ public static ISystemWebAdapterBuilder AddSessionSerializer(this ISystemWebAdapt
2828

2929
return builder;
3030
}
31-
32-
public static ISystemWebAdapterBuilder AddJsonSessionSerializer(this ISystemWebAdapterBuilder builder, Action<JsonSessionSerializerOptions>? configure = null)
33-
{
34-
if (builder is null)
35-
{
36-
throw new ArgumentNullException(nameof(builder));
37-
}
38-
39-
builder.AddSessionSerializer();
40-
builder.Services.TryAddSingleton<ISessionKeySerializer, JsonSessionKeySerializer>();
41-
42-
var options = builder.Services.AddOptions<JsonSessionSerializerOptions>();
43-
44-
if (configure is not null)
45-
{
46-
options.Configure(configure);
47-
}
48-
49-
return builder;
50-
}
5131
}

src/Microsoft.AspNetCore.SystemWebAdapters.SessionState/Serialization/SessionValues.Shared.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)