.NET CORE 2.0 Unable to resolve service for type while attempting to activate

28,070

Please ensure you register dependencies in DI container inside Startup.cs

public class Startup
{  
    ...

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddScoped<IDataRepository<Token, long>, TokenManager>();
        ...
    }
}
Share:
28,070
Jason
Author by

Jason

Updated on November 21, 2020

Comments

  • Jason
    Jason over 3 years

    I have a IDataRepository.cs file that contains an interface and its implementation like so:

    public interface IDataRepository<TEntity, U> where TEntity : class
    {
        IEnumerable<TEntity> GetAll();
        TEntity Get(U id);
        TEntity GetByString(string stringValue);
        long Add(TEntity b);
        long Update(U id, TEntity b);
        long Delete(U id);
    }
    

    I have another class TokenManager.cs that implements IDataRepository Interface:

    public class TokenManager : IDataRepository<Token, long>
    {
        ApplicationContext ctx;
        public TokenManager(ApplicationContext c)
        {
            ctx = c;
        }
    
        //Get the Token Information by ID
        public Token Get(long id)
        {
            var token = ctx.Token.FirstOrDefault(b => b.TokenId == id);
            return token;
        }
    
        public IEnumerable<Token> GetAll()
        {
            var token = ctx.Token.ToList();
            return token;
        }
    
    
        //Get the Token Information by ID
        public Token GetByString(string clientType)
        {
            var token = ctx.Token.FirstOrDefault(b => b.TokenClientType == clientType);
            return token;
        }
    
        public long Add(Token token)
        {
            ctx.Token.Add(token);
            long tokenID = ctx.SaveChanges();
            return tokenID;
        }
    
    }
    

    and finally, I have a controller to put all things together, my controller files looks like this:

    [Route("api/[controller]")]
    public class TokenController : Controller
    {
        private IDataRepository<Token, long> _iRepo;
        public TokenController(IDataRepository<Token, long> repo)
        {
            _iRepo = repo;
        }
    
        // GET: api/values  
        [HttpGet]
        public IEnumerable<Token> Get()
        {
            return _iRepo.GetAll();
        }
    
        // GET api/values/produccion
        [HttpGet("{stringValue}")]
        public Token Get(string stringValue)
        {
            return _iRepo.GetByString(stringValue);
        }
    }
    

    But the problem is that every time I try to access some method from my API, for example using postman I get the error:

    InvalidOperationException: Unable to resolve service for type FECR_API.Models.Repository.IDataRepository`2[FECR_API.Models.Token,System.Int64] while attempting to activate;FECR_API.Controllers.TokenController

    I tried using something like this inside ConfigureServices, but get a conversion error

    services.AddScoped<IDataRepository, TokenManager>();
    

    Any idea what I'm doing wrong?