title: HttpContext.Session in Blazor Server Application
body: <p>I am trying to use <code>HttpContext.Session</code> in my ASP.NET Core Blazor Server application (as described in this <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-3.1" rel="nofollow noreferrer">MS Doc</a>, I mean: all correctly set up in startup)</p>
  
  <p>Here is the code part when I try to set a value:</p>
  
  <pre><code>var session = _contextAccessor.HttpContext?.Session;
  if (session != null &amp;&amp; session.IsAvailable)
  {
      session.Set(key, data);
      await session.CommitAsync();
  }
  </code></pre>
  
  <p>When this code called in Razor component's <code>OnAfterRenderAsync</code> the <code>session.Set</code> throws following exception:</p>
  
  <blockquote>
    <p>The session cannot be established after the response has started.</p>
  </blockquote>
  
  <p>I (probably) understand the message, but this renders the Session infrastructure pretty unusable: the application needs to access <strong>its state</strong> in every phase of the execution...</p>
  
  <p><strong>Question</strong></p>
  
  <p>Should I forget completely the <code>DistributedSession</code> infrastructure, and go for Cookies, or Browser SessionStorage? ...or is there a workaround here still utilizing <code>HttpContext.Session</code>? I would not want to just drop the distributed session infra for a way lower level implementation... </p>
  
  <p>(just for the record: Browser's Session Storage is NOT across tabs, which is a pain)</p>