17

I'm trying to migrate an ASP.NET MVC site to ASP.NET Core with the .NET Core runtime. Previously we could get objects out of the session store, even in different assemblies, with

var obj = HttpContext.Current.Session[key]

Now I've read we must inject IHttpContextAccessor and use the method on _contextAccessor.HttpContext.Session. But the methods of the Session object have changed, and it no longer has indexing applied.

I have seen people in other questions using HttpContext.Session.GetString() and HttpContext.Session.SetString():

Access HttpContext.Current

With these I could at least de/serialize the object I want to fetch/get. But I can't find these methods on the interface.

'ISession' does not contain a definition for 'GetString' and no extension method 'GetString' accepting a first argument of type 'ISession' could be found

How to I get access to these methods?

DavidG
  • 113,891
  • 12
  • 217
  • 223
zola25
  • 1,774
  • 6
  • 24
  • 44
  • 2
    **Pro tip**: Hitting Ctrl+. on the error message should suggest adding the correct namespace for extension methods and/or package (if not already loaded and the method parameters are correct). – Tseng Jul 01 '17 at 12:33

1 Answers1

40

GetString is an extension method in the Microsoft.AspNetCore.Http.Extensions assembly, make sure you have a reference to that. You may also need to import it:

using Microsoft.AspNetCore.Http;
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 6
    Also, if you're doing this in a .NET Standard library (i.e. without the `Microsoft.AspNetCore.All` NuGet), you need the `Microsoft.AspNetCore.Http.Extensions` NuGet or you're still get this error even after adding the using line. – Chris Pratt Dec 20 '17 at 17:54
  • Another possible cause for this error message - you are using the System.ServiceModel.Channels for your ISession instead of the Microsoft.AspNetCore.Http ISession. Make sure you remove the ServiceModel Channels reference or specify which type of ISession you want to reference. – Eric Conklin Mar 08 '23 at 18:43