0
votes

I'm attempting to create an ASP.NET Core app using a generic repository pattern using Entity Framework along with a service layer. However, whenever I start up my project I get hit with this error:

Unable to resolve service for type 'GameSource.Data.Repositories.GameRepository' while attempting to activate 'GameSource.Services.GameService'.

Unable to resolve service for type GameRepository while attempting to activate GameService

GamesController.cs:

    public class GamesController : Controller
    {
        private IGameService gameService;

        public GamesController(IGameService gameService)
        {
            this.gameService = gameService;
        }
    }

GameService.cs:

    public class GameService : IGameService
    {
        private GameRepository gameRepo;

        public GameService(GameRepository gameRepo)
        {
            this.gameRepo = gameRepo;
        }
    }

GameRepository.cs:

    public class GameRepository : BaseRepository<Game>, IGameRepository
    {
        private GameSource_DBContext context;
        private DbSet<Game> gameEntity;

        public GameRepository(GameSource_DBContext context) : base(context)
        {
            this.context = context;
            gameEntity = context.Set<Game>();
        }
     }

BaseRepository.cs:

    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private GameSource_DBContext context;
        private DbSet<T> entity;

        public BaseRepository(GameSource_DBContext context)
        {
            this.context = context;
            entity = context.Set<T>();
        }
    }

GameSource_DBContext.cs:

    public class GameSource_DBContext : DbContext
    {
        public GameSource_DBContext(DbContextOptions<GameSource_DBContext> options) : base(options)
        {
        }

        public DbSet<Game> Game { get; set; }
        public DbSet<Genre> Genre { get; set; }
        public DbSet<Developer> Developer { get; set; }
        public DbSet<Publisher> Publisher { get; set; }
        public DbSet<Platform> Platform { get; set; }
    }

Startup.cs:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews().AddRazorRuntimeCompilation();

            services.AddDbContext<GameSource_DBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("GameSource_DB")));

            services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
            services.AddScoped<IGameRepository, GameRepository>();
            services.AddScoped<IGameService, GameService>(); //Can't resolve service?
        }

As you can see in the startup, I'm trying to inject my service as well as my repository, but it can't resolve the service for the GameRepository when trying to activate GameService. Am I missing something? Is this the right approach to use with Entity Framework i.e. repo + service layers?

Thank you for your patience.

1
Please do not post images of exceptions and stack traces. Instead post the stack trace's text into your question. - Steven

1 Answers

2
votes

In the GameService change it so it uses the IGameRepository interface like so:

public class GameService : IGameService
{
    private IGameRepository gameRepo;

    public GameService(IGameRepository gameRepo)
    {
        this.gameRepo = gameRepo;
    }
}

FYI, GameRepository can't be resolved because you've configured the dependency injection to inject the repository when the interface IGameRepository is used in the constructor not the concrete implementation.