2

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.

Lasanga Guruge
  • 832
  • 1
  • 5
  • 15

1 Answers1

1

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 have 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.

AFract
  • 8,868
  • 6
  • 48
  • 70
  • 1
    Thanks for your advice mate. Ill probably use both. For repos and services Autofac and Ill go for serviceCollecion for services that are well compatible with serviceCollection. For an instance, it would be complicated to register consumers for MassTransit through Autofac. – Lasanga Guruge Dec 21 '22 at 10:50
  • 1
    I have never attempted to use both in the same app. Obviously there is no reason why this should not work, but it could be a little harder to maintain, at least the time for the developer to guess that you are using two different DI framework. But I understand your point. Altough, have you noticed that there is a MassTransit.Autofac nuget package that is probably intended to handle your case ? See package itself, and https://stackoverflow.com/a/32972665/461444 for example – AFract Dec 21 '22 at 14:32
  • Yes, I went through this package but their doc encourages to use the IServiceCollection itself and the Masstransit.Autofac nuget wasn't updated since January. – Lasanga Guruge Dec 22 '22 at 03:17
  • https://masstransit-project.com//usage/containers/autofac.html – Lasanga Guruge Dec 22 '22 at 03:23