LoginSignup
0
1

More than 1 year has passed since last update.

【ASP.NET Core】セッションに保存できる文字列長の限界を検証してみた

Posted at

課題

ASP.NET Coreで利用できるセッション(Microsoft.AspNetCore.Http.HttpContext.Session)にはどのくらいの長さの文字列を格納できるのか?

サイズオーバーを起こすと後々大変なので、検証してみることにしました。

結論

1,000万字の1バイト文字(約10GB)の文字でも収めることができました。

但し、セッションはマシンメモリを消耗しますので、これほどのサイズを格納する前に設計の見直しが必要でしょう。

とはいえ、多少長い程度の情報であれば問題なく格納できることが分かりました。

(参考)検証に使用した環境とコード

環境は.NET6.0、Docker+Linuxでプロジェクトを作成しています。

セッションに格納した後、リダイレクト先で表示します。

HomeController.cs

public IActionResult Index() {

    var sb = new System.Text.StringBuilder();
    for (int i = 0; i < 10000000; i++) {
        sb.Append('a');
    }

    //10,000,000文字の変数をセッションに保存
    HttpContext.Session.SetString("hoge", sb.ToString());

    return RedirectToAction("Result");
}

public IActionResult Result() {
    //10,000,000文字の変数をセッションから取り出して表示
    ViewBag.Result = HttpContext.Session.GetString("hoge");

    return View();
}

Razor(Result.cshtml)

<p>
    @ViewBag.Result
</p>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1