1

I am using ASP.NET Core 2.1 in a WPF application for providing a REST API access to the application. The application uses Autofac and several modules are already registered at the start of the application. Following the Autofac quickstart guide ("With ConfigureContainer"), I added the DependencyInjection NuGet package to the ASP.NET Core project and configured it in the part where the WebHostBuilder is created via

.ConfigureServices(services => services.AddAutofac())

However, the dependency injection is not working in the ASP.NET Core controller. I am sure I am missing a configuration step, so any hints how the already registered components can be used?

UPDATE: I followed the guide "Adding a custom dependency injection container in ASP.NET Core" and populated the ASP.NET Core dependencies into an own Container, however, this Container does not know the Container nor the LifetimeScope of the already working instance of Autofac of the main application.

3
  • sorry but i do not follow why you are using asp.net mvc set up to simply provide a restful api access...maybe i have mis-read this.
    – jazb
    Oct 26, 2018 at 8:08
  • We have more than just a restful API working (but at the moment this is where we start with) in the background and wanted to use a flexible and extendable system in our application. ASP.NET Core provides exactly this.
    – Octoate
    Oct 26, 2018 at 8:11
  • Can you provide your startup class for context or where you register your services with the autofac container?
    – cl0ud
    Oct 29, 2018 at 9:11

1 Answer 1

1

I think I came across the same issue. I'm not at my computer to perfectly reproduce your problem, so I can update this later if it doesn't work for you, but:

I added .UseServiceProviderFactory(new AutofacServiceProviderFactory()) after ConfigureServices.

I found this from this blog post.

1
  • Hi, thanks for your answer. I tried to use it, but I am using WebHostBuilder.CreateDefaultBuilder() and it seems that it is not possible to use .USeServiceProviderFactory(...) on it. I was able to register the ASP.NET Core stuff in an own container via the tutorial "Adding a custom dependency injection container in ASP.NET Core". But in the new ContainerBuilder doesn't know the one of the application, that was already registered.
    – Octoate
    Oct 29, 2018 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.