(CORE) CORE (2022)

Notes about singleton service.

My current project now has 3 singleton services and I want small notice about this type of Net Core services.



First interesting experiment is create simple Singleton class in OOP meaning (with private constructor what protect to create another class instance and shared variable refer to instance) and try to use this class as Net Core singleton service, something like that:



This is not workable way because ASP.NET DI container forbid this way to create singleton.



This is interesting result - singleton class and singleton NET Core service is not related one to another entity.

So, correct way is class with public constructor visible from DI container.



We can delegate create exemplar of singleton service to DI container (line 62).



And moreover, in my case I want to use singleton service as BLL, as service over other controller function (not only for classes), therefore I must inject reference to other services in methods, not in constructor.



And main future to create singleton service - pay attention how I inject instance of singleton service to controller in order to avoid cross reference in DI container.



Comments ( )
Link to this page: http://www.vb-net.com/SingletonService/Index.htm
< THANKS ME>