In ASP.NET MVC 5 is possible to obtain some dependency through DependencyResolver.Current.GetService<T>()
. Is there something similar in ASP.NET Core?
A dependency resolver is just a service locator integrated with the ASP.NET MVC codebase. Resolvers are a way to add the implementation of the Dependency Inversion principle into an existing (large) codebase.
You can inject an instance of type IServiceProvider into any method of a class. You can also take advantage of the ApplicationServices property of the IApplicationBuilder interface and the RequestServices property of the HttpContext class to retrieve an IServiceProvider instance.
The IServiceProvider is responsible for resolving instances of types at runtime, as required by the application. These instances can be injected into other services resolved from the same dependency injection container. The ServiceProvider ensures that resolved services live for the expected lifetime.
Yes, there is. In ASP.NET Core 1.0.0, the services available within a request from HttpContext
are exposed through the RequestServices collection[1]:
this.HttpContext.RequestServices
You can use the GetService method to retrieve the dependencies by specifying the type of the dependency:
this.HttpContext.RequestServices.GetService(typeof(ISomeService));
Generally, you shouldn’t use these properties directly, preferring instead to request the types your classes you require via your class’s constructor, and letting the framework inject these dependencies. This yields classes that are easier to test and are more loosely coupled.
[1] https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#request-services
If you really need it, you can write own one. First - create AppDependencyResolver
class.
public class AppDependencyResolver { private static AppDependencyResolver _resolver; public static AppDependencyResolver Current { get { if (_resolver == null) throw new Exception("AppDependencyResolver not initialized. You should initialize it in Startup class"); return _resolver; } } public static void Init(IServiceProvider services) { _resolver = new AppDependencyResolver(services); } private readonly IServiceProvider _serviceProvider; public object GetService(Type serviceType) { return _serviceProvider.GetService(serviceType); } public T GetService<T>() { return _serviceProvider.GetService<T>(); } private AppDependencyResolver(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } }
Please note that _serviceProvider.GetService<T>();
only available if you add using Microsoft.Extensions.DependencyInjection;
. That namespace will be available if you add "Microsoft.Extensions.DependencyInjection": "1.0.0"
to your project.json
. Than you should call Init
method in your startup
class. For example
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { AppDependencyResolver.Init(app.ApplicationServices); //all other code
After that you can use it anywhere, same as DependencyResolver.Current
. But my suggestion - use it only if no other choice left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With