Autofac: when to use RegiserType with SingleInstance vs RegisterInstance with ExternallyOwned?
For example if I have a global singleton for a Redis cache provider and want to also register it in DI.
I could instantiate it then register it with RegisterInstance and ExternallyOwned, or I could use any register method followed by SingleInstance.
Which one do you decide is better?
3 Replies
It's been a while since I used Autofac but I think IIRC the big distinction was the disposal behavior
If a dependency is registered as externally owned, it will not be disposed once the container is disposed of
The container is never disposed though right?
I think the asp net core container is just like the microsoft DI one, it lives with the scope of your app
But you could technically build a container yourself and dispose it anywhere