2

I have an issue with the static method in ASP.NET. I created the Singleton below. During the execution process I will call Context.getInstance() several times and I need the same context value. But, once I make another request (Get, Post, wherever) to the server I need a new context because my context is dependant from .NET HttpContext. But, unfortunately once I call getInstance for the first time the class never will be instantiated again.

Any ideas how I solve this problem?

public class Context
{
   private static Context _context = null;

   private Context()
   { ... }

   public static Context getInstance()
   {
       if (Context._context == null)
           _context = new Context();

       return _context;
   }
}

4 Answers 4

4

Get rid of your static variable and store it in HttpContext.Current.Items.

public static Context GetInstance()
{
    if (HttpContext.Current.Items["MyContext"] == null) 
    {
        HttpContext.Current.Items["MyContext"] = new Context();
    }
    return (Context)HttpContext.Current.Items["MyContext"];
}
Sign up to request clarification or add additional context in comments.

4 Comments

This would be the best way to go.
Add (Context) in the return statement.
I implemented this code in my project and seems work. So, every new request I do this HttpContext.Items will start again and it is not shared with another requests, right? If yes, this answer my question.
@Joao Gilberto, pretty much yes, every new request gets a new HttpContext.Items collection. One exception is if you do a Server.Transfer then the continuation will share the same HttpContext.Items but then it's also debatable whether or not it is considered a new request or the continuation of the same request.
0

If I understand you correctly, you need the context only in a single page request, correct? If so, the method above certainly won't work - the static will live for the life of the app domain. This lifetime can vary depending on a number of factors. I would start by creating a base page class (inheriting from the core ASP.NET Page class) and include a Context property on it. Since the page only "lives" for one request, that should work for you.

Comments

0

Another approach - I prefer using my own ThreadStatic variables (ala HttpContext.Current) rather than use the HttpContext Items collections simply because I think (an opinion) that it makes for cleaner code. YMMV.

public class Context
{
    [ThreadStatic()]
    private static Context _Context = null;

    private HttpContext _HttpContext = null;

    public Context()
    {
        _HttpContext = HttpContext.Current;
    }

    public static Context Current
    {
        if(_Context == null || 
           _HttpContext != _HttpContext.Current)
        {
            _Context = new Context();
        }
        return _Context;
    }
}

2 Comments

ThreadStatic is NOT SAFE IN ASP.NET. hanselman.com/blog/… Besides that, you cannot access non-static _HttpContext within static property Current.
I tested your code and looks like it works. But I have a concern about security issues. My context never can be shared with another request and I read on several places this is not safe in ASP.NET.
0

if your variable is static then all user will access that same variable and any change to that variable will effect all users in case of web-application only, the logic is when your are making a variable static then a memory location is allocated in server when this location is allocated all users share that location only because it is static.

Comments

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.