0

I am new to the ASP.Net framework, and I am currently struggling to understand how to manage lifetimes of objects.

In a more specific case I am currently facing a problem in my application:

I want to have a class starting with my application, which should run in the background and do some work which results in some data my application controllers are working with later on.

I would therefore try to create this class' object in my Global.asax.cs, so it can start and run as soon as my application is running as well.

However, how can I pass this instance to controllers which might get called later on then?

Currently, my only idea would be to make my data-collection class static, which I am not really happy with, as I would like to avoid static classes as much as possible.

Is there any solution to this?

Sossenbinder
  • 4,852
  • 5
  • 35
  • 78
  • Maybe you could use Singleton: https://msdn.microsoft.com/en-us/library/ff650316.aspx – Anthony McGrath Mar 04 '18 at 21:36
  • 1
    Configure your controllers for dependency injection, then register your instance as a singleton with your DI container. This will permit much greater flexibility in the future, as all your controllers know about is a class (or, preferably, an interface) that they get supplied with in their constructor. – RB. Mar 04 '18 at 21:40
  • @RB. Thank you! I heard about Ninject already, I think I will take a look at this – Sossenbinder Mar 04 '18 at 21:45
  • 2
    [XY Problem](http://xyproblem.info/). I would call this a red flag. If you have a [crosscutting concern](https://msdn.microsoft.com/en-us/library/ee658105.aspx), the ideal place to put it in ASP.NET MVC is in a globally registered [filter](https://msdn.microsoft.com/en-us/library/gg416513(VS.98).aspx) so you don't have to do something in every controller. See [this answer](https://stackoverflow.com/a/36224308). – NightOwl888 Mar 04 '18 at 21:46
  • BTW - the asp.net tag is general and does not imply which UI framework you are using. Please specify the UI framework you are using (asp.net-mvc, asp.net-core-mvc, razor-pages, webforms, etc). – NightOwl888 Mar 04 '18 at 21:51
  • The state of the art pattern for this sort of thing is to use Dependency Injection and specify instance per process. You might want to look at [this question](https://softwareengineering.stackexchange.com/questions/285339/share-service-instances-amongst-all-controllers). DI may not be available without a third party package like [AutoFac](https://autofac.org/) (depending on your version of MVC). – John Wu Mar 04 '18 at 22:22

3 Answers3

1

You could create a class with static members or alternatively a singleton to hold data and logic in one place. The processing inside the class could be started by a call to one of its methods in Global.asax.cs in Application_Start.

Since more than one thread may access static data collection simultaneously, the data collection may require multi-thread access handling.

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
1

Options from top of my head:

Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
  • Thanks, these are some good suggestions. I am having trouble fitting my knowledge about usual C# programming in the whole lifecycle of ASP.Net applications. The application variable route seems great. – Sossenbinder Mar 04 '18 at 21:43
1

If you are using a dependency injection framework such as Autofac, you could instantiate a new instance of your class and register it as a single instance (singleton) with the DI framework.

// In your DI config -- Autofac used here as an example
Foo myFoo = new Foo();
myFoo.Start();
builder.RegisterInstance<Foo>(myFoo).AsSelf().SingleInstance();

Then, just add it as an argument to the constructor of your controllers.

public class HomeController : Controller
{
    private readonly Foo _myFoo;

    public HomeController(Foo myFoo)
    {
        _myFoo = myFoo;
    }
}