redux17
redux17

Reputation: 685

Dependeny Injection Error Unable to resolve service for type while attempting to activate

I'm getting Dependency Injection Eror when I try POST data via Postman.

This is code in controller and service file.

DashboardController.cs

using AutoMapper;
using System.Threading.Tasks;

namespace Web.API.Controllers
{
    [Route("api/dashboards")]
    public class DashboardController : Controller
    {
        private readonly IDashboardService dashboardService;

        public DashboardController(IDashboardService dashboardService)
        {
            this.dashboardService = dashboardService;
        }

        /// <summary>
        /// Describe this method here.
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> CreateDashboard([FromBody]Dashboard dashboardResource)
        {
            try
            {
                if (!ModelState.IsValid)
                    return BadRequest(ModelState);

                var createdDashboard = await dashboardService.Create(dashboardResource);
                return Ok(createdDashboard);
            }
            catch (Exception)
            {
                return BadRequest();
            }
        }
    }
}

DashboardService.cs

using System.Threading.Tasks;

namespace Domain.Services
{
    public class DashboardService: IDashboardService
    {
        private readonly IRepository<Dashboard> dashboardRepository;

        public DashboardService(IRepository<Dashboard> dashboardRepository)
        {
            this.dashboardRepository = dashboardRepository;
        }

        public async Task<Dashboard> Create(Dashboard dashboard)
        {
            var createdDashboard = await dashboardRepository.Create(dashboard);
            return createdDashboard;
        }
    }
}

In Startup method ConfigureServices I've addded AddScoped

Startup.cs

public void ConfigureServices(IServiceCollection services)
            {
                services.AddAutoMapper();

                services.AddScoped<IDashboardService, DashboardService>();
                services.AddScoped<IRepository<Dashboard>, DashboardRepository>();

                services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));

                services.AddMvc();

                services.AddTransient<DatabaseDeployer>();
            }

I'm not sure what I did wrong. I use N-tier arhitecture with repository, automapper etc..

DashboardRepository.cs

using AutoMapper;

namespace Repositories.Repositories
{
    public class DashboardRepository : IRepository<domain.Dashboard>
    {
        private readonly DbContext context;
        private readonly IMapper mapper;

        public DashboardRepository(DbContext context, IMapper mapper)
        {
            this.context = context;
            this.mapper = mapper;
        }

        public async Task<domain.Dashboard> Create(domain.Dashboard dashboard)
        {
            var entityToCreate = mapper.Map<domain.Dashboard, dbModels.Dashboard>(dashboard);

            var createdEntity = context.Add(entityToCreate);
            await context.SaveChangesAsync();

            var createDashboard = mapper.Map<dbModels.Dashboard, domain.Dashboard>(dashboard); 
        }

Upvotes: 0

Views: 11725

Answers (2)

Baqer Naqvi
Baqer Naqvi

Reputation: 6504

In my case, i forgot to inject dependecny in startup.cs

Public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<ISyncRequestRepository, SyncRequestManager>();     
}

Upvotes: 0

tchelidze
tchelidze

Reputation: 8308

Looks like you have referenced incorrect Dashboard in Startup and DashboardRepository class.

DashboardRepository.cs

public class DashboardRepository : IRepository<Domain.Entities.Dashboard.Dashboard> 

Startup.cs

services.AddScoped<IRepository<Domain.Entities.Dashboard.Dashboard>, DashboardRepository>();

Note

Look at DashboardRepository class, you have defined

using domain = Domain.Entities;

but are using Dashboard class directly

public class DashboardRepository : IRepository<Dashboard>

instead of

public class DashboardRepository : IRepository<domain.Dashboard>

So, Dashboard refers to Repositories.Models.Dashboard, since you have added following using statement

using Repositories.Models;

Upvotes: 4

Related Questions