N
N
Nikita2012-05-24 17:37:01
ASP.NET
Nikita, 2012-05-24 17:37:01

Asp.net singleton?

Good afternoon, habravchane, I encountered such a problem:
Asp.net mvc3 has quite a standard singleton class, in which something is put on request and something is withdrawn every 30 minutes in another thread.
The problem is that the thread created in application_start sees the singleton as clean, and all user requests as full (as it should be). What could be wrong? Why can't I get the correct singleton instance in the thread.
Actually, here he is, quite standard:

private static MemoryManager _instance;

        public static MemoryManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MemoryManager();
                }
                return _instance;
            }
        }

*I need it as a replacement HttpApplicationState
Update : Not relevant anymore. Tear off the author's hands and tie them to the battery.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Shedal, 2012-05-24
@Shedal

From MSDN:

The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.
That is, your thread is most likely created in a separate process - not in the one in which your particular HttpApplication instance is spinning. Try creating the thread in the Init()
method instead , it should help.

A
Alexey Prokhorov, 2012-05-24
@megahertz

I could be wrong, because I'm not a .NET specialist, but maybe it's thread-safety? habrahabr.ru/post/125421/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question