Best place to create a global object in ASP.NET MVC

I would like to implement a ConcurrentQueue object in my ASP.NET MVC application. The ConcurrentQueue object will be shared between sessions and must be created once. What is the best place to create a ConcurrentQueue in ASP.NET MVC?

+4
source share
3 answers

Any class you select can contain an instance of it, however it would be more reasonable to link it in a class that is responsible for any functionality in which the queue is used.

For example, the Cache class:

public class MyCache { public static ConcurrentQueue Queue { get; private set; } static MyCache() { Queue = new ConcurrentQueue(); } } 

This will initialize it the first time you use the MyCache class. If you want finer grain control, you can create an Initialize method that calls your Global.asax.cs file when the application starts.

+7
source

You can:

  • Create it in a static constructor, so it is created only when the type is used in some code
  • Global.asax.
  • Use WebActivator - you will not pollute the Global.asax file, and you can create a queue in another assembly.
+3
source

File Global.asax.cs , protected void Application_Start() method overload.

Another approach would be to create a Singleton / static class.

+2
source

Source: https://habr.com/ru/post/1399018/


All Articles