0

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

6
  • 1
    Well what is IDataContextAsync? Commented May 19, 2017 at 12:21
  • You've given me the Clue mate!! :) Commented May 19, 2017 at 12:29
  • 6
    Well the clue was in the exception message, I'll never understand why people ignore them! Commented May 19, 2017 at 12:31
  • 1
    See this for some working code: deviq.com/repository-pattern Commented May 20, 2017 at 0:36
  • 1
    Also if you want to wire up your generic repo generically: ardalis.com/… Commented May 20, 2017 at 0:37

1 Answer 1

1

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://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.