In an ASP.NET MVC, when the application start, I need to some connections (LDAP, …) in background. Is there a way to do this “thread safe” in a ASP.NET MVC application ?
Thanks,
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should avoid running background tasks in AS.NET. But if for some reason you need to do it, you could start a new thread in
Application_Startand perform those tasks. SinceApplication_Startexecutes only once, this thread will run once (unless of course you configure some timer -> which you probably should not be doing).If on the other hand you need to consume the work done in this background thread from your ASP.NET MVC controllers (which run inside the context of an HTTP request) you will need proper synchronization in order to ensure that the initialization thread you started in
Application_Starthas finished. Depending on your exact scenario there might be different ways to achieve that.