Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate adapters session logic from core package #496

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/SystemWebAdapters/Microsoft.AspNetCore.SystemWebAdapters.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F9DB9323-C91
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{A1BDA50C-D70B-416C-97F1-74B0649797C5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SystemWebAdapters.SessionState", "src\Microsoft.AspNetCore.SystemWebAdapters.SessionState\Microsoft.AspNetCore.SystemWebAdapters.SessionState.csproj", "{2029D409-07E3-49F8-BB6A-77114DE7B337}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SystemWebAdapters.SessionState.Tests", "test\Microsoft.AspNetCore.SystemWebAdapters.SessionState.Tests\Microsoft.AspNetCore.SystemWebAdapters.SessionState.Tests.csproj", "{9AFF3DCE-5DEF-4337-B5BC-C98ABEA6BEDC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -50,6 +54,14 @@ Global
{B1D06F62-B315-4ED8-8109-168B4D4E4B86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1D06F62-B315-4ED8-8109-168B4D4E4B86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1D06F62-B315-4ED8-8109-168B4D4E4B86}.Release|Any CPU.Build.0 = Release|Any CPU
{2029D409-07E3-49F8-BB6A-77114DE7B337}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2029D409-07E3-49F8-BB6A-77114DE7B337}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2029D409-07E3-49F8-BB6A-77114DE7B337}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2029D409-07E3-49F8-BB6A-77114DE7B337}.Release|Any CPU.Build.0 = Release|Any CPU
{9AFF3DCE-5DEF-4337-B5BC-C98ABEA6BEDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9AFF3DCE-5DEF-4337-B5BC-C98ABEA6BEDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9AFF3DCE-5DEF-4337-B5BC-C98ABEA6BEDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9AFF3DCE-5DEF-4337-B5BC-C98ABEA6BEDC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -60,6 +72,8 @@ Global
{11DD4D64-7A95-4635-A273-775715E18852} = {95915611-30BF-4AFF-AE41-5CDC6F57DCF7}
{174A36F1-27ED-43FC-A3A1-00DA58C4E30C} = {95915611-30BF-4AFF-AE41-5CDC6F57DCF7}
{B1D06F62-B315-4ED8-8109-168B4D4E4B86} = {95915611-30BF-4AFF-AE41-5CDC6F57DCF7}
{2029D409-07E3-49F8-BB6A-77114DE7B337} = {F9DB9323-C919-49E8-8F96-B923D2F42E60}
{9AFF3DCE-5DEF-4337-B5BC-C98ABEA6BEDC} = {A1BDA50C-D70B-416C-97F1-74B0649797C5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DABA3C65-9D74-4EB6-9B1C-730328710EAD}
Expand Down
7 changes: 4 additions & 3 deletions src/SystemWebAdapters/samples/ClassLibrary/SessionUtils.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Microsoft.AspNetCore.SystemWebAdapters.SessionState.RemoteSession;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState;

namespace ClassLibrary;

public class SessionUtils
{
public static void RegisterSessionKeys(RemoteAppSessionStateOptions options)
public static string ApiKey = "test-key";

public static void RegisterSessionKeys(SessionOptions options)
{
options.ApiKey = "test-key";
options.RegisterKey<int>("test-value");
options.RegisterKey<SessionDemoModel>("SampleSessionItem");
}
Expand Down
6 changes: 5 additions & 1 deletion src/SystemWebAdapters/samples/MvcApp/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ protected void Application_Start()

Application.AddSystemWebAdapters()
.AddProxySupport(options => options.UseForwardedHeaders = true)
.AddRemoteSession(ClassLibrary.SessionUtils.RegisterSessionKeys);
.AddRemoteAppSession(options=>
{
options.ApiKey = ClassLibrary.SessionUtils.ApiKey;
ClassLibrary.SessionUtils.RegisterSessionKeys(options);
});
}
}
}
4 changes: 4 additions & 0 deletions src/SystemWebAdapters/samples/MvcApp/MvcApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@
<Content Include="Scripts\jquery-3.4.1.min.map" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters.SessionState\Microsoft.AspNetCore.SystemWebAdapters.SessionState.csproj">
<Project>{2029d409-07e3-49f8-bb6a-77114de7b337}</Project>
<Name>Microsoft.AspNetCore.SystemWebAdapters.SessionState</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters\Microsoft.AspNetCore.SystemWebAdapters.csproj">
<Project>{55c1bbe0-b922-46b0-8f2c-8472bc9a5f33}</Project>
<Name>Microsoft.AspNetCore.SystemWebAdapters</Name>
Expand Down
1 change: 1 addition & 0 deletions src/SystemWebAdapters/samples/MvcCoreApp/MvcCoreApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters.SessionState\Microsoft.AspNetCore.SystemWebAdapters.SessionState.csproj" />
<ProjectReference Include="..\ClassLibrary\ClassLibrary.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/SystemWebAdapters/samples/MvcCoreApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState;

var builder = WebApplication.CreateBuilder();
builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
Expand All @@ -10,6 +9,7 @@
.AddRemoteAppSession(options =>
{
options.RemoteApp = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = ClassLibrary.SessionUtils.ApiKey;

ClassLibrary.SessionUtils.RegisterSessionKeys(options);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netcoreapp3.1;net472</TargetFrameworks>
<HasImplementation>false</HasImplementation>
<LangVersion>10</LangVersion>
<IsPackable>true</IsPackable>
<Nullable>enable</Nullable>
<EnablePackageValidation>true</EnablePackageValidation>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.AspNetCore.SystemWebAdapters.SessionState.Tests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" Key="0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7" />
</ItemGroup>

<ItemGroup>
<FrameworkFiles Include="**/*.Framework.cs" />
<StandardFiles Include="**/*.Standard.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<Compile Remove="**/*" />
<Compile Include="@(StandardFiles)" />
<Compile Include="RemoteSession/RemoteAppSessionStateOptions.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'netcoreapp3.1' ">
<Compile Remove="@(FrameworkFiles)" />
<Compile Remove="@(StandardFiles)" />

<FrameworkReference Include="Microsoft.AspNetCore.App" />

<Using Include="Microsoft.AspNetCore.Http.HttpContext" Alias="HttpContextCore" />
<Using Include="Microsoft.AspNetCore.Http.HttpResponse" Alias="HttpResponseCore" />
<Using Include="Microsoft.AspNetCore.Http.HttpRequest" Alias="HttpRequestCore" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<Compile Remove="**/*" />
<Compile Include="@(FrameworkFiles)" />
<Compile Include="RemoteSession/RemoteAppSessionStateOptions.cs" />
<Compile Include="Serialization/SessionValues.cs" />
<Compile Include="Serialization/SerializedSessionState.cs" />
<Compile Include="Serialization/SessionSerializer.Shared.cs" />

<Reference Include="System.Web" />

<PackageReference Include="System.Text.Json" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.AspNetCore.SystemWebAdapters\Microsoft.AspNetCore.SystemWebAdapters.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState.RemoteSession;

namespace Microsoft.AspNetCore.SystemWebAdapters;

public static class RemoteAppSessionStateExtensions
{
public static ISystemWebAdapterBuilder AddRemoteAppSession(this ISystemWebAdapterBuilder builder, Action<RemoteAppSessionStateOptions> configure)
{
var options = new RemoteAppSessionStateOptions();
configure(options);
builder.Modules.Add(new RemoteSessionModule(options));
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.AspNetCore.SystemWebAdapters.SessionState.RemoteSession;

public class RemoteAppSessionStateOptions
public class RemoteAppSessionStateOptions : SessionOptions
{
internal const string ApiKeyHeaderName = "X-SystemWebAdapter-RemoteAppSession-Key";
internal const string ReadOnlyHeaderName = "X-SystemWebAdapter-RemoteAppSession-ReadOnly";
Expand Down Expand Up @@ -49,18 +49,6 @@ public class RemoteAppSessionStateOptions
#endif
public string CookieName { get; set; } = "ASP.NET_SessionId";

/// <summary>
/// Gets the mapping of known session keys to types
/// </summary>
public IDictionary<string, Type> KnownKeys { get; } = new Dictionary<string, Type>();

/// <summary>
/// Registers a session key name to be of type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
public void RegisterKey<T>(string key) => KnownKeys.Add(key, typeof(T));

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// The maximum time loading session state from the remote app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using System;
using System.Web;
using System.Web.SessionState;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState.RemoteSession;

namespace Microsoft.AspNetCore.SystemWebAdapters.Modules;
namespace Microsoft.AspNetCore.SystemWebAdapters.SessionState.RemoteSession;

internal sealed class RemoteSessionModule : IHttpModule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using System.Web;

namespace Microsoft.AspNetCore.SystemWebAdapters.Modules;
namespace Microsoft.AspNetCore.SystemWebAdapters;

internal class ProxyHeaderModule : IHttpModule
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.SystemWebAdapters.Modules;
namespace Microsoft.AspNetCore.SystemWebAdapters;

public class ProxyOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;

namespace Microsoft.AspNetCore.SystemWebAdapters.SessionState;

/// <summary>
/// An interface to register known keys for session objects.
/// </summary>
public class SessionOptions
{
/// <summary>
/// Gets the mapping of known session keys to types
/// </summary>
public IDictionary<string, Type> KnownKeys { get; } = new Dictionary<string, Type>();

/// <summary>
/// Registers a session key name to be of type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
public void RegisterKey<T>(string key) => KnownKeys.Add(key, typeof(T));
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.AspNetCore.SystemWebAdapters.Modules;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState.RemoteSession;

namespace Microsoft.AspNetCore.SystemWebAdapters;

Expand All @@ -28,9 +25,6 @@ public static ISystemWebAdapterBuilder AddSystemWebAdapters(this HttpApplication
public static ISystemWebAdapterBuilder AddProxySupport(this ISystemWebAdapterBuilder builder, Action<ProxyOptions> configure)
=> builder.AddModule(configure, static options => new ProxyHeaderModule(options));

public static ISystemWebAdapterBuilder AddRemoteSession(this ISystemWebAdapterBuilder builder, Action<RemoteAppSessionStateOptions> configure)
=> builder.AddModule(configure, static options => new RemoteSessionModule(options));

internal static ISystemWebAdapterBuilder? GetSystemWebBuilder(this HttpApplicationState state)
=> state[Key] as ISystemWebAdapterBuilder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
<StandardFiles Include="**/*.Standard.cs" />
</ItemGroup>

<ItemGroup>
<SharedFiles Include="Adapters/SessionState/SessionOptions.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<Compile Remove="**/*" />
<Compile Include="@(StandardFiles)" />
<Compile Include="Adapters/SessionState/RemoteSession/RemoteAppSessionStateOptions.cs" />
<Compile Include="@(SharedFiles)" />
</ItemGroup>

<ItemGroup Condition="$(HasImplementation)">
Expand All @@ -46,16 +50,9 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<Compile Remove="**/*" />
<Compile Include="@(FrameworkFiles)" />
<Compile Include="Adapters/SessionState/RemoteSession/RemoteAppSessionStateOptions.cs" />
<Compile Include="Adapters/SessionState/Serialization/SessionValues.cs" />
<Compile Include="Adapters/SessionState/Serialization/SerializedSessionState.cs" />
<Compile Include="Adapters/SessionState/Serialization/SessionSerializer.Shared.cs" />
<Compile Include="@(SharedFiles)" />

<Reference Include="System.Web" />
<Reference Include="System.Configuration" />

<PackageReference Include="System.Text.Json" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
Comment on lines -55 to -58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main goal was to remove these dependencies? What problems did we expect these to cause people?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That the general HttpContext adapter usage could be done without a bunch of new dependencies. People often complain when their packages.config ends up with a ton of new packages (which is what this will do to a .NET 4.7.2 app). Since they may not need the session stuff, this can be a pay-for-play thing.

</ItemGroup>

<Import Condition="$(HasImplementation)" Project="GenerateApis.targets" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace System.Runtime.CompilerServices;

internal static class IsExternalInit
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac.Extras.Moq" Version="6.0.0" />
<PackageReference Include="AutoFixture" Version="4.15.0" />
<PackageReference Include="Moq" Version="4.16.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<Compile Remove="IsExternalInit.cs" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.AspNetCore.Http.HttpContext" Alias="HttpContextCore" />
<Using Include="Microsoft.AspNetCore.Http.HttpRequest" Alias="HttpRequestCore" />
<Using Include="Microsoft.AspNetCore.Http.HttpResponse" Alias="HttpResponseCore" />

<Using Include="System.Web.HttpContext" Alias="HttpContext"/>
<Using Include="System.Web.HttpRequest" Alias="HttpRequest"/>
<Using Include="System.Web.HttpResponse" Alias="HttpResponse"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters.SessionState\Microsoft.AspNetCore.SystemWebAdapters.SessionState.csproj" />
</ItemGroup>

</Project>