Adding Autofac to .NET core 6.0 using the new single file template

I found this Microsoft docs var builder = WebApplication.CreateBuilder(args); builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); // Register services directly with Autofac here. // Don’t call builder.Populate(), that happens in AutofacServiceProviderFactory. builder.Host.ConfigureContainer<ContainerBuilder>( builder => builder.RegisterModule(new MyApplicationModule())); var app = builder.Build();

ASP.NET Core MediatR error: Register your handlers with the container

I have met the same issue. The problem is that this line code services.AddMediatR(typeof(AddEducationCommand).GetTypeInfo().Assembly); handles all the MediatR IRequest and IRequestHandlers. but you created an IRepository interface and its implementation class which can’t be handled by that MediatR.Extensions.Microsoft.DependencyInjection so keep all your changes but add this – manually register this like services.AddScoped(typeof(IUniversityRepository), typeof(UniversitySqlServerRepository)); then issue … Read more

What is the difference between DependencyResolver.SetResolver and HttpConfiguration.DependencyResolver in WebAPI

Prevent mixing MVC and Web API in the same project. Microsoft seems to suggest this, because the Visual Studio template for Web API mixes the project automatically with MVC, but this is a bad idea. From an architectural point of view, MVC and Web API are completely different. MVC is a UI technology aimed to … Read more

Translate ninject ISecureDataFormat binding to Autofac

The migration from Ninject to Autofac seems to be correct, but the binding for the ISecureDataFormat<AuthenticationTicket> interface may not be working as expected, leading to an activation error in some dependencies. Based on the debugging messages, the BearerTokenCookieStore class is failing to resolve this dependency. In the Ninject version, ISecureDataFormat<AuthenticationTicket> is bound to a method … Read more

Simple Injector vs Hiro vs Autofac [closed]

Let me start by saying that I’m the lead developer behind Simple Injector. I agree with Mark that in most cases performance of a container isn’t a problem. Still, some containers perform very poor at some points and it can be hard to intuitively sense what parts of the configuration can be problematic from a … Read more

Autofac RegisterInstance vs SingleInstance

Is the manual newing-up of the ServiceProductDataProvider with RegisterInstance not the same as the Register .SingleInstance() ?? The RegisterInstance allows you to register a single instance in AutoFac. The difference between RegisterInstance and RegisterType + SingleInstance methods is that the RegisterInstance method allows you to register an instance not built by Autofac. But both solution … Read more

How is AutoFac better than Microsoft.Extensions.DependencyInjection?

It’s a fair question; fundamentally the difference is in the additional features Autofac can offer you if you need them. For simple applications, the Microsoft DI may offer enough functionality, but I’ve found as my application grows there’s some extra features I find myself wanting/needing. The features that in my mind encouraged me to originally … Read more

Passing parameters to constructors using Autofac

You can always use the WithParameter method to explicitly specify a constructor parameter: builder.RegisterType<DoesSomething>() .As<IDoesSomething>() .WithParameter(“helper”, new HelperClass(“do”, “something”)); builder.RegisterType<DoesSomethingElse>() .As<IDoesSomethingElse>() .WithParameter(“helper”, new HelperClass(“do”, “somethingelse”)); As far as I can tell there is no need for an interface for HelperClass because it essentially is just a value holder. For this to work you would need … Read more

Is it possible to configure Autofac to work with ASP.NET MVC and ASP.NET Web Api

It is certainly possible to configure Autofac to work with both MVC and Web API. This is expected to be a very common scenario. There are two separate dependency resolver implementations because MVC and Web API can be used independently of one another. The same applies for the Autofac integrations. When using both MVC and … Read more