0

I have a class with static functions which need to use the current HttpContext. Instead of sending the object each time I use these functions, I tried a different approach. I built such a property:

private static HttpContext _http;
private static HttpContext http
{
    get
    {
        if (_http == null)
           _http = HttpContext.Current;
        return _http;
    }
    set { _http = value; }
}

In the functions I use http as my HttpContext.

The thing it it seems unstable. Is there some problem with this?

UPDATE: I found this works nicely

    get
    {
        return _http ?? HttpContext.Current;
    }

Explanations please

JNF
  • 3,696
  • 3
  • 31
  • 64

1 Answers1

0

What on Earth made you to create a setter and a backing field? Wouldn't it be much easier to have only the getter, which returns a current http context?

protected static HttpContext Http
{
    get
    {
        return HttpContext.Current;
    }
}

By the way:

I do like much private properties, you could either make it protected for the ancestors or make it a method:

private static HttpContext GetCurrentContext() 
{
    return HttpContext.Current;
}
AgentFire
  • 8,944
  • 8
  • 43
  • 90
  • I don't want it to change once it was set so the context stays the same across different modules. I think. Been a while, I don't really remember myself. – JNF Jul 10 '13 at 08:03
  • @JNF The context changes the next time you accept incoming request. – AgentFire Jul 10 '13 at 10:23