title: Cannot access Session object inside a helper class within my Asp.Net Core project
body: <p>I am trying to access the HttpContext.Session object in a helper class within my ASP.NET Core 2.1 project.</p>
  
  <p>When I try to access HttpContext.Session I get the following error.</p>
  
  <blockquote>
    <p>CS0120    An object reference is required for the non-static field, method, or property 'HttpContext.Session' </p>
  </blockquote>
  
  <p>With .NET 4.x ASP.NET, it was easily accessed with "HttpContext.Current.Session".</p>
  
  <p>Here is my class:</p>
  
  <pre><code> public class MySession
   {
  
      public void Foo()
      {
          HttpContext.Session.SetString("Name", "The Doctor"); // will not work
          HttpContext.Session.SetInt32("Age", 773);  // will not work
  
      }
   }
  </code></pre>
  
  <p>Here is my Startup.cs:</p>
  
  <pre><code>    public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddDistributedMemoryCache();
  
              services.AddSession(options =&gt;
              {
                  // Set a short timeout for easy testing.
                  options.IdleTimeout = System.TimeSpan.FromMinutes(30);
                  options.Cookie.HttpOnly = true;
              });
  
              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
              // In production, the Angular files will be served from this directory
              services.AddSpaStaticFiles(configuration =&gt;
              {
                  configuration.RootPath = "ClientApp/dist";
              });
  
              services.Configure&lt;ServiceSettings&gt;(Configuration.GetSection("ServiceSettings"));
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  app.UseExceptionHandler("/Error");
                  app.UseHsts();
              }
  
              app.UseSession();
              app.UseHttpsRedirection();
              app.UseStaticFiles();
              app.UseSpaStaticFiles();
  
              app.UseMvc(routes =&gt;
              {
                  routes.MapRoute(
                      name: "default",
                      template: "{controller}/{action=Index}/{id?}");
              });
  
              app.UseSpa(spa =&gt;
              {
                  // To learn more about options for serving an Angular SPA from ASP.NET Core,
                  // see https://go.microsoft.com/fwlink/?linkid=864501
  
                  spa.Options.SourcePath = "ClientApp";
  
                  if (env.IsDevelopment())
                  {
                      spa.UseAngularCliServer(npmScript: "start");
                  }
              });
          }
      }
  </code></pre>
  
  <p>Do I need to inject something into the MySession class?</p>