Sphyrnidae Common Library  2.0.1
Shared Utilities/Library
Sphyrnidae.Common.Utilities.ServiceLocator Class Reference

Obtains a service registered in the DI framework More...

Static Public Member Functions

static T Get< T > (IServiceProvider sp)
 

Detailed Description

Obtains a service registered in the DI framework

.net Core no longer supports service locator in the same fashion that .net framework did. Eg. in .net FW, you could directly access the global DependencyResolver and it would give you the correct thing. In .net Core, you can't directly access this, and an attempt to "save it off" fails as well. This fails because this is saving off the root scope, which effectively turns scoped items into singletons. I have found no other way than to unfortunately rely solely on DI - either injecting the services directly, Or injecting IServiceProvider and doing a lookup that way. Either way, you have to DI something, so you might as well DI things directly instead of IServiceProvider. But I'll leave this method here in case you only want to pass around 1 object instead of everything.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0::scope-validation

Member Function Documentation

◆ Get< T >()

static T Sphyrnidae.Common.Utilities.ServiceLocator.Get< T > ( IServiceProvider  sp)
static

The documentation for this class was generated from the following file: