원격 앱 세션 상태

원격 앱 세션 상태를 사용하면 ASP.NET Core 앱과 ASP.NET 앱 간의 통신을 통해 세션 상태를 검색할 수 있습니다. 이는 세션 상태를 검색하고 설정하기 위해 쿼리할 수 있는 ASP.NET 앱에 엔드포인트를 노출하여 사용 설정됩니다.

HttpSessionState 직렬화

원격 앱 세션 상태를 사용하도록 설정하려면 HttpSessionState 개체를 직렬화해야 합니다. 이 작업은 기본 이진 작성기 구현이 제공되는 형식 Microsoft.AspNetCore.SystemWebAdapters.SessionState.Serialization.ISessionSerializer의 구현을 통해 수행됩니다. 이는 다음 코드에 의해 추가됩니다.

builder.Services.AddSystemWebAdapters()
    .AddSessionSerializer(options =>
    {
        // Customize session serialization here
    });

구성

먼저 원격 앱 설정 지침에 따라 ASP.NET Core 및 ASP.NET 앱을 연결합니다. 그런 다음 원격 앱 세션 상태를 사용하도록 설정하기 위해 호출할 몇 가지 추가 확장 메서드가 있습니다.

ASP.NET Core 구성에는 AddRemoteAppSessionAddJsonSessionSerializer 호출을 포함하여 알려진 세션 항목 유형을 등록해야 합니다. 그러면 코드가 다음과 비슷해야 합니다.

builder.Services.AddSystemWebAdapters()
    .AddJsonSessionSerializer(options =>
    {
        // Serialization/deserialization requires each session key to be registered to a type
        options.RegisterKey<int>("test-value");
        options.RegisterKey<SessionDemoModel>("SampleSessionItem");
    })
    .AddRemoteAppClient(options =>
    {
        // Provide the URL for the remote app that has enabled session querying
        options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);

        // Provide a strong API key that will be used to authenticate the request on the remote app for querying the session
        options.ApiKey = builder.Configuration["RemoteAppApiKey"];
    })
    .AddSessionClient();

세션 지원에는 ASP.NET Core 파이프라인에 대한 추가 작업이 필요하며 기본적으로 켜져 있지 않습니다. ASP.NET Core 메타데이터를 통해 경로별로 구성할 수 있습니다.

예를 들어 세션 지원을 사용하려면 컨트롤러에 주석을 추가해야 합니다.

[Session]
public class SomeController : Controller
{
}

또는 기본적으로 모든 엔드포인트에 대해 사용하도록 설정하려면 다음을 수행합니다.

app.MapDefaultControllerRoute()
    .RequireSystemWebAdapterSession();

동일한 프레임워크는 Global.asax.cs에서 다음과 같이 변경됩니다.

SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
    .AddJsonSessionSerializer(options =>
    {
        // Serialization/deserialization requires each session key to be registered to a type
        options.RegisterKey<int>("test-value");
        options.RegisterKey<SessionDemoModel>("SampleSessionItem");
    })
    // Provide a strong API key that will be used to authenticate the request on the remote app for querying the session
    // ApiKey is a string representing a GUID
    .AddRemoteAppServer(options => options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"])
    .AddSessionServer();

프로토콜

읽기 전용

Readonly 세션은 어떤 종류의 잠금도 없이 프레임워크 앱에서 세션 상태를 검색합니다. 이는 세션 상태를 반환하고 즉시 닫을 수 있는 단일 GET 요청으로 구성됩니다.

Readonly session will retrieve the session state from the framework app

쓰기 가능

쓰기 가능한 세션 상태 프로토콜은 읽기 전용과 동일하게 시작하지만 다음과 다릅니다.

  • 상태를 업데이트하려면 추가 PUT 요청이 필요합니다.
  • 초기 GET 요청은 세션이 완료될 때까지 열려 있어야 합니다. 닫힌 경우 세션을 업데이트할 수 없습니다.

Writeable session state protocol starts with the same as the readonly