Avoid Debugging This Autofac Headache in ASP NET Core
March 18, 2024
• 772 views
dependency injectionautofaciocinversion of control c#autofac asp.net coreautofac dependency injection web api c#dependency injection c# autofacc# autofac tutorialinversion of controldependency inversion principle tutorialc# autofac dependency injectionautofac tutorialautofac dependency injection c#dependency injection c# exampledotnetasp.net core dependency injectiondependency injection in asp.net corehow to use dependency injection in asp.net core
Dependency injection is incredibly valuable for us when working with our ASP NET Core applications. By default, we get access to the IServiceCollection interface to be able to configure our dependencies, but I like using Autofac for dependency injection instead.
The setup for Autofac in ASP NET Core is very simple, but there's a mistake that I make every single time. In this video, I'll walk you through what that mistake is and the easy solution for it. You'll be using Autofac and know how to use dependency injection in ASP.NET Core in no time at all!
View Transcript
when we're building asp.net core applications one of the things that we get right out of the box is a dependency injection framework however one of the go-to libraries that I like to use for this is autofac hi my name's Nick centino and I'm obsessed with autoag wait no scratch it that's wrong hi my name's Nick centino and I'm a principal software engineering manager at Microsoft in this video I'm going to show you how you can set up autofac inside of your asp.net core application and I'm going to highlight the one mistake that I make literally every single time I go to do this and spend about 20 minutes trying to figure out what I'm doing wrong in fact just over this past weekend I had this problem and I figured it's time to make a video to reinforce this in my head a quick reminder
before I jump over to visual studio to check out that pin comment for a link to my free newsletter and my courses on dome train and with that said let's go check out this code okay so what I've done to start with is just taken the sample weather application that you get when you create a new asp.net core app now all that I've done from there is a couple of things one is going to be adding the nougat package which I'll show you in just a moment but I taken the code that used to have the summaries for the different weather that would be generated it's all kind of just dummy data right so I've taken that I pulled it into a dedicated class just called my dependency and you can see that it just has this hardcoded summaries collection here and then that gets
returned out now that gets used right here on this API and I've just passed it in as a parameter so by doing this with the dependency injection framework it should be able to resolve that we have my dependency on the container that's the first part the next part is of course the nougat packages that I've mentioned I'm just going to jump into the project file itself and we can see that I'm including autofac extensions dependency injection this Nate package also includes autofac that's what you're going to need as well but why we need this extensions one is because we want to be able to add it onto the services that we have for our Builder here now even the template app comes with this comment that says at this point in time you want to add services to your container and usually how this would
work is if you were just using the I service collection you would type Builder doservices and then you would do whatever you need to do to go add your dependencies onto this container however I like using autofac and that's why in this video we're going to see how we can hook up autofac such that it works with this dependency injection container now when we're working with autofac it has its own container Builder class it looks something like this here you make a new container Builder and then you could go register your dependencies kind of like what I've just written right here but to make that work with the I service collection we have an extension method that we can leverage now the obvious one that's available to us is ADD autofac that's available on this services collection here and because I said we need to
use a container Builder how would we bank one and pass that into the autofac setup well instead of thinking about it that way you flip it around on its head and you use the container Builder that it's giving you so that way you do all of your registration inside of here if you were doing plug-in loading and stuff like that you you could scan for assemblies inside of this method as well register all of the modules whatever you want to do this is the method that gives you the container Builder and really at this point we should be able to go press play and have our minimal API working once this is hooked up we should be able to have this get automatically resolved because it will pick it up from the I service collection thanks to this wiring up of the container Builder pretty
simple right well the problem is that it doesn't work and unfortunately this is what gets me stuck every single time because I try to figure out why is it not resolving it properly the first thing we'll notice is that it's suggesting do we need to see if we need to label these things properly with attributes so I end up going to spend some time on that I've taken the from service attribute I could stick that out the front here and try running it again because technically this is a dependency from the service collection and it's not coming say from the request body or the URL or any query parameters so if I go run this now that should work certainly right and well no same type of issue different error it says no service for type my dependency has been registered but that's really weird
because I'm pretty confident we just registered it up here using what's given to us with ADD autofac at this point I generally go back to the internet and I start searching around and maybe that's exactly what you did to get here the other thing that I end up doing is I find one of the projects that I already have working like this and I go oh oh yeah that's what I had to do and the hint is really right here when I hover over with my cursor it says in capital letters only for pre asp.net 3.0 hosting this won't work for asp.net core 3.0 and above or generic hosting and every single time I forget to read this and that's because it's not very obvious rate from this method name the way that we solve this is that we don't use this at all so
we delete that code and then like I said generally what I do is I go copy and paste the code that's in a working project and that's right here it looks very similar but what we have instead is this used service provider Factory and then it takes in the autofax service provider Factory and we do configure container right on top of here and again we get a container Builder given to us so we're not providing one to it but we use the one that it's providing us another thing to note is that this is right on the host instead of being on the do Services property of the Builder the other great thing is that we don't even need this attribute anymore and at this point in time if I go run this using these two lines on this setup instead of just the one
that we saw before that explicitly said add autofac we go run this we should have a weather app that functions now and while it's not super exciting at least this now works and we were able to hook up autofac to our asp.net core application and there we go that's a quick video on how you can wire up autofac to be your dependency injection framework in asp.net core the thing that you'll need to start off is the Nate package from autofac which has the extensions for dependency injection that'll allows you to wire it up to the I service collection or in this particular case to do it properly directly onto the host from there you're going to not want to use the ADD autofac extension method on the service collection but instead there's two other extension methods for you to use one is the Ed service
provider Factory and then you want to pass in the autofac configuration there and then the second part is configure container and that's where you're going to get a container Builder given to you and you can go register the things that you need to directly on that Auto container from there it all starts working like magic once again with dep pendency injection and we can start leveraging the services that we want directly on our minimal apis or in the constructors of the types that we're creating that are also part of that dependency injection container if you found this helpful and want to see another video about some common challenges using reflection and dependency injection you can check out this video next thanks and I'll see you next time
Frequently Asked Questions
What is the main mistake to avoid when setting up Autofac in ASP.NET Core?
The main mistake to avoid is using the 'Add Autofac' extension method on the service collection. Instead, you should use the 'UseServiceProviderFactory' method and pass in the Autofac service provider factory, followed by 'ConfigureContainer' to get the container builder.
Do I need to use attributes like [FromServices] when using Autofac?
No, you don't need to use attributes like [FromServices] when setting up Autofac correctly. Once you configure it properly with the right methods, it will automatically resolve your dependencies.
What NuGet package do I need to install to use Autofac with ASP.NET Core?
These FAQs were generated by AI from the video transcript.You need to install the 'Autofac.Extensions.DependencyInjection' NuGet package, which includes Autofac and allows you to integrate it with the ASP.NET Core dependency injection framework.
