4

I am trying to access data saved in Session state, in an ASP.Net Core Web Application, outside the controller, but the httpcontext is always null, how do I send the state over to a class?

I have added the correct statements in Startup.cs, to use sessions.

Furthermore, using this inside the controller works perfectly fine:

HttpContext.Session.SetString("Threshold",threshold);
HttpContext.Session.GetString("Treshold");

both work completely fine when accessing within the controller, yet I want to access this data in another class. Currently I am just using a static variable, but this is of course not the way to go, I want to access the session in here:

public class ImageAnalysisExtensionValues
{
    public static double ConfidenceThreshold { get; set; }
}

(Data has been converted to double).

What do I do?

croxy
  • 4,082
  • 9
  • 28
  • 46
Zwac
  • 67
  • 1
  • 5

1 Answers1

3

You can make use of Asp.Net Cores dependency injection and use the IHttpContextAccessor interface.

You have to register it first in your Startup.cs class (it is not always registered as default - therefore the use of TryAddSingleton<>()):

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    // ...
}

Then use it like this:

public YourClassOutsideOfController
{
    private IHttpContextAccessor _contextAccessor;

    public YourClassOutsideOfController(IHttpContextAccessor contextAccessor)
    {
        _contextAccessor = contextAccessor;
    }

    private void YourMethod()
    {
        var context = _contextAccessor.HttpContext;
        context.Session.SetString("Threshold",threshold);
        context.Session.GetString("Threshold");
    }
}
croxy
  • 4,082
  • 9
  • 28
  • 46
  • And If my class already has a constructor, just add it as a new one? But how do I send the information from the controller? It's just there automatically due to session? – Zwac Aug 21 '18 at 13:15
  • @Zwac What are the parameters for the existing constructor? Would it be possible to obtain them also via dependency injection? The thing is while using dependency injection you have to use it top to bottom. – croxy Aug 21 '18 at 13:46
  • Have in mind, that the class I'm trying to access this in is the serialized model class. I have added two constructors, one that accepts a IHttpContextAccessor and one that has 0 args. I don't need to serialize the constructors do I? I always get a "null" when trying to access the context value, so I'm assuming it just uses the 0 arg constructor. – Zwac Aug 22 '18 at 07:01
  • @Zwac What do you mean by serialized model class? Where does it come from? – croxy Aug 22 '18 at 07:06
  • I can't inherit the interface ISession either, there must be some workaround to using static variables that works for me. – Zwac Aug 22 '18 at 07:08
  • @Zwac It seems like dependency injection isn't an option in your case. Another possibility would be, If you are instantiating your class in a controller you can pass the HttpContext into the constructor of your class. Or if you are accessing the HttpContext in a method of this class which you call from a controller, pass the HttpContext as parameter to the method. I do not have enough information about your model and how you use it for any further suggestions. – croxy Aug 22 '18 at 07:15
  • I apologize. It is due to the fact that I am using the image analysis in ComputerVision API, this is the transfer object in which I am trying to access the session value: https://i.imgur.com/hSXFNZM.png – Zwac Aug 22 '18 at 07:43
  • I wouldn't access the HttpContext in the transfer object at all. As the name states this object should only be used as container for transferring data from one to another point. It shouldn't hold any logic on where this data comes from. Get the ConfidenceThreshold somewhere else and pass it into your transfer object while creating it (if I understand this correctly you are creating the object, filling it and then sending it to the api?). – croxy Aug 22 '18 at 07:56
  • Yes exactly, the only problem is that I can't "send" information to the ImageAnalysis since it's part of the ComputerVisionAPI, this is the only way I could make it work. I have resolved my issue regarding Sessions via this post: https://stackoverflow.com/questions/37785767/how-to-access-the-session-in-asp-net-core-via-static-variable?rq=1. Sorry to bother, as well as asking the wrong questions. – Zwac Aug 22 '18 at 08:18
  • Sorry I couldn't help you any better. I had a few trouble understanding your question correctly. I'm glad you found a solution to your problem. – croxy Aug 22 '18 at 08:22
  • 1
    It is impossible to answer a question so vague, I just had no idea how to explain it, yet you made me realize what I was really looking for. For that, I thank you. – Zwac Aug 22 '18 at 08:26