C
C#6mo ago
BlackHorizon

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
Sossenbinder
Sossenbinder6mo ago
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
BlackHorizon
BlackHorizonOP6mo ago
The container is never disposed though right?
Sossenbinder
Sossenbinder6mo ago
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

Did you find this page helpful?