.net-6.0autofac

Autofac IComponentContext vs IServiceProvider


I have been using Autofac for a while and I find it really useful in dynamic resolving services in runtime and also the lifetimeScope feature. Moreover, I find the Autofac modules more cleaner way of registering services for DI over registering them in startup/extensions. Now Im migrating few projects from .net-core-3.1 to net6 and found IServiceProvider has the ability to resolve services dynamically in runtime. Also since IServiceProvider wraps IServiceScopeFactory it has the ability to create disposable scopes as well. Im using few dependencies that must be registered through IServiceCollection like MassTransit. In that case I think sticking to one IoC container would be good for the long run.

Should I completely remove Autofac and stick with IServiceCollection?

Tried going through many official documentations. Just trying to find the best solution out of the two ways.


Solution

  • Have you seen for example this question: How is AutoFac better than Microsoft.Extensions.DependencyInjection??

    While Microsoft.Extensions.DependencyInjection was a great improvement to .NET platform and still improves at each new version (as you have seen), Autofac is a old product with a lot of very nice features that are not (yet) supported by MS native solution.

    Also, you might also think about support of existing code base and developers who are used to Autofac.

    For a completely new project, if Microsoft.Extensions.DependencyInjection has all the features you need, I think you can go for it. It works very well and it's native, so every .NET developer should at least know it, rather than a specific DI framework. But if you need specific features from Autofac (sorry, no comprehensive comparison to provide but you can probably find some and the answer link I posted above will already give you some ideas), it's still a very relevant and powerful solution that will probably remain supported for a long time.