リモート アプリのセッション状態

リモート アプリのセッション状態により、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();

Protocol

読み取り専用

読み取り専用セッションは、何らかのロックなしでフレームワーク アプリからセッション状態を取得します。 これは、セッション状態を返す単一の 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