Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core DependencyResolver

Tags:

In ASP.NET MVC 5 is possible to obtain some dependency through DependencyResolver.Current.GetService<T>(). Is there something similar in ASP.NET Core?

like image 893
analyser Avatar asked Jun 14 '16 13:06

analyser


People also ask

What is Dependencyresolver?

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.

Can you inject IServiceProvider?

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.

What is IServiceProvider .NET Core?

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.


2 Answers

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

like image 55
Sergio Vicente Avatar answered Oct 06 '22 06:10

Sergio Vicente


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.

like image 34
YuriyP Avatar answered Oct 06 '22 06:10

YuriyP