Search code examples
asp.net-mvcdependency-injection

Dependency Injection in .net 4.7?


I'm a little confused as to what integrated options I have for DI. I see it's pretty straightforward for .net core (for my particular projects), but I don't need to build a cross platform app and don't see the advantage to using core. However, it doesn't look like .net framework applications are still setup with Global.asax and without Startup.cs so does that mean there is no integrated DI option for .net framework 4.7? Do I still need to get a 3rd party solution or is there a way to use the same DI workflow in a .net framework project as is used in a core project?


Solution

  • Dependency Injection is not integrated by default in classic asp.net, you need to add a nuget package to handle DI (only integrated by default in asp.net core and later versions).

    Snipsnipsnip added an implementation example below with a Microsoft nuget package. Many 3rd party packages work too. Back in the days, the most popular ones were Autofac and SimpleInjector. Tutorials can be found on https://autofac.readthedocs.io/en/latest/getting-started/index.html or https://docs.simpleinjector.org/en/latest/quickstart.html