Reputation: 3747
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?
Upvotes: 21
Views: 45592
Reputation: 495
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
Upvotes: 13
Reputation: 3747
EDIT: Even though I found out how to do it as explained below, I still ended up going with Autofac because I didn't realize the Microsoft's solution only supports constructor injection, but not property injection.
I found instructions on how to do it here. I know link answers are bad, but I don't have time to do any more than this. If someone else wants to make an answer with full instructions I will mark it.
https://scottdorman.blog/2016/03/17/integrating-asp-net-core-dependency-injection-in-mvc-4/
Also note that if you are not using Owin already, it is not required. You can set it up just the same in Application_Start
method of Global.asax
. Only change you would need to make is when it references the Startup
class in a statement that reflectively gets all the Controller classes, you will need to change that to be the class the code is in (or any other class in your assembly).
Upvotes: 6