System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: YOURSERVICE: Singleton ImplementationType: TheRepositoryConsumingTheInterface : Cannot consume scoped service 'DBContext' from singleton 'YourInterface' 


Question: How do you solve for "System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: YOURSERVICE: Sington ImplementationType: TheRepositoryConsumingTheInterface : Cannot consume scoped service 'DBContext' from sington 'YourInterface' 



Login to See the Rest of the Answer

Answer:
Make sure that you make other Services Consuming Singleton Services as Singleton as well. You cannot Consume Singleton Services injected into Dependency Injection Container from a Scopped or Transiented Dependency Injector. That is a no no, If you want to make couple Services as Singleton (that gets instatiented once per Application Life Time) then make sure down the chain of enheritace or consumption the Services consuming Singleton Services are also Singleton.

You cannot Consume a Singleton Injected Service from a Scopped or Transient Injected Service because every time you make a request a Scoped Service is instatieted and all Depedencies made available to that Scopped Service via a Constructor will also need to be instantiated, hence a Singleton gets instatieted only once. So this creates a conflict on resolving the Dipendecy, the Compiler gets sort of mad, saying, "hey, if you wanted to make Singleton Service a Scopped then you should have said so" Don't make me instantiet a Singleton Service once an Application Life Time then tell me to Instatiete it again? how dare you!!!!


Why would you want to make your Services Singleton? you ask. Well, if your Services are getting disposed everytime your request enters the Event Handler, your application losses state hence all Depedencies injeced as Scopped, Transients gets disponsed as well. When your deligate function (eventHapped += myBeutifulEventHandlerFunction ) or an Event Handler calls another function in a different Service to process the values received from the event emitted and that function in a different Service need to get access to the application settings or config file to get necessary settings to further process the request, the function can nolonger get access to the config file, because that config file was injected as a Scopped/Singleton Service. 

Unlike above, if you injected the Config Service as a Singleton, the Service would be available to access regardless wheather the applicaton lost state about all Scopped Services.







For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]