1

So I basically have an event that fired off when my connected cash receiver takes in money, and I'm using a session variable to store the index of the bill inserted, and I get an error that says: Session Variable error

My session variables work everywhere else correctly except here. Any idea why this is happening? Everywhere I look online it tells me to set up my session correctly which it is if my other ones work fine. This event is fired when on a view page with no other methods running. Any help is appreciated. If the information is too vague I apologize, I will provide more information if you need it to help me solve this problem. Thank you.

Edit: Stack call stack

This is the event being fired where the SetString is causing the error.

 void validator_OnCredit(object sender, CreditArgs e)
    {
        Console.WriteLine("Credited bill#: {0}", e.Index);

        switch (e.Index)
        {
            case 1:
                HttpContext.Session.SetString("Test", "1");
                break;
            default:
                HttpContext.Session.SetString("Test", e.Index.ToString());
                break;
        }

        Thread.Sleep(500);
    }
3
  • can you post the stack? what type of page is it (Generic Web handler)? Commented Aug 22, 2016 at 4:16
  • It's a razor page, regular MVC stuff. Commented Aug 22, 2016 at 4:22
  • @timkly I edited my post and put up a screen shot of the call stack and some details on the broken session. I hope this is what you had in mind, otherwise I apologize Commented Aug 22, 2016 at 4:24

1 Answer 1

2

3) Updated Answer Session Namespace Reference for beta(s) or RC1

Microsoft.AspNet.Session

Session Namespace Reference from CORE 1.0

Microsoft.AspNetCore.Session

2) Updated Answer - If accessing a session outside the controller then you need to inject the session into it as follows

public class TestClass
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private ISession _session => _httpContextAccessor.HttpContext.Session;

    public TestClass(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public void SetString()
    {
        _session.SetString("Test", "Ben Rules!");
    }

    public string GetString()
    {
        return _session.GetString("Test");
    }
}

1) Initial Answer - Incorrect for current scenario... but if session isn't working this is a good place to start

From the error message I assume you are using .NET Core?

If so then the middleware is added sequentially so you need to ensure that your code intitialises Session Management first before it uses MVC

public void ConfigureServices(IServiceCollection services)
{
   // ...
   services.AddSession();
   // ...
}

public void Configure(IApplicationBuilder app)
{
    app.UseSession();
    app.UseMvc();
    // ...
}
Sign up to request clarification or add additional context in comments.

7 Comments

As I said in my question, I have already set up the sessions like that. My other session variables across the program work fine. Its just in this event where it doesn't.
Actually looking at the signature for the page is that a web form? If so, Web Forms are tied to System.Web will remain in the .NET 4.x framework... which means the MVC app and the Web Forms probably won't be able to share session state
No it's an asp.net web application. Rc-1
Can you post up a snippet of the controller?
Of what specifically in the controller?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.