I was resolving service using like this ServiceProvider.GetService<T>()
. But then i found that Resolving Scoped Serivices was getting runtime error during calling the method ServiceProvider.GetService<T>()
Saying service not found in root provider. Then i found to resolve creating scope like this,
using (var scope = _serviceProvider.CreateScope())
{
return scope.ServiceProvider.GetService<T>();
}
I want to create a generic function. But the problem is i don't know when to use service provider scope and when not to use service provider as i don't know the service lifetime type(Scoped/Singleton). How can i create this generic function that will resolve the service without knowing the lifetime or resolving the lifetime issue?