简体   繁体   中英

Resolving Dependency in Asp.Net Core

I am working on a generic repository pattern on EF Core and I have a problem resolving the dependency of my Repository on Asp.Net Core. Using Unity i am Able to Solve the dependency here are my code:

Using Unity

 //here is the Line I Cant Resolve
.RegisterType<IRepositoryAsync<Movie>, Repository<Movie>>()

.RegisterType<IMovieService, MovieService>()

Asp.Net Core

services.AddScoped<IRepositoryAsync<Movie>, Repository<Movie>>();

services.AddTransient<IMovieService, MovieService>();

and I am getting Error

InvalidOperationException: Unable to resolve service for type 'Repository.Pattern.DataContext.IDataContextAsync' while attempting to activate 'Repository.Pattern.Core.Repository`1[Sample.Models.Movie]'.

Can anyone has a clear documentation on how Native Dependency Injection works on Asp.Net Core

Microsoft.Extensions.DependencyInjection uses constructor injection to inject dependencies into resolved services. Your service locator needs to know how to construct these dependencies to produce the requested service.

Very basically if you want to resolve IMyService with a default constructor of IMyService(IMyOtherService otherService) you need to register IMyOtherService as well.

In your example you need to register Repository.Pattern.DataContext.IDataContextAsync in order to resolve IRepositoryAsync<Movie> .

Comprehensive documentation is here https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM