locked
One or more constructors in Home Controller - error RRS feed

  • Question

  • User-945256609 posted

    Dear all,

    I am getting the following error in asp.net core project. Can somebody let me know how to overcome this error.

    InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'EmployeeManagement.Controllers.HomeController'. There should only be one applicable constructor.

    • Microsoft.Extensions.DependencyInjection.ActivatorUtilities.TryFindMatchingConstructor(Type instanceType, Type[] argumentTypes, ref ConstructorInfo matchingConstructor, ref Nullable<int>[] parameterMap)

    • Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, out ConstructorInfo matchingConstructor, out Nullable<int>[] parameterMap)

    • Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes)

    • Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.CreateActivator(ControllerActionDescriptor descriptor)

    • Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.CreateControllerFactory(ControllerActionDescriptor descriptor)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerCache.GetCachedResult(ControllerContext controllerContext)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider.OnProvidersExecuting(ActionInvokerProviderContext context)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ActionInvokerFactory.CreateInvoker(ActionContext actionContext)

    • Microsoft.AspNetCore.Mvc.Routing.ActionEndpointFactory+<>c__DisplayClass7_0.<CreateRequestDelegate>b__0(HttpContext context)

    • Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)

    • Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)

    • Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

    code in home controller

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    using EmployeeManagement.Models;

    namespace EmployeeManagement.Controllers
    {
    public class HomeController : Controller
    {
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
    _logger = logger;
    }

    private readonly IEmployeeRepository _employeeRepository;

    public HomeController(IEmployeeRepository employeeRepository)
    {
    _employeeRepository = employeeRepository;
    }

    /*public IActionResult Index()
    {
    return View();
    }*/
    public string Index()
    {
    return _employeeRepository.GetEmployee(1).Name;
    }

    public IActionResult Privacy()
    {
    return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
    return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
    }
    }

    Tuesday, May 5, 2020 3:02 AM

All replies

  • User-945256609 posted

    I had commented logger constructor. then the code started working

    Tuesday, May 5, 2020 3:12 AM
  • User1686398519 posted

    Hi,  sathyaav

    As prompted by the error message you gave: There should only be one applicable constructor.

    You should put "IEmployeeRepository" and "ILogger" in a constructor.You could modify your code like this:

    public HomeController(ILogger<HomeController> logger, IEmployeeRepository employeeRepository)
    
            {
    
                _logger = logger;
    
                _employeeRepository = employeeRepository;
    
            }

    Best Regards,

    YihuiSun

    Tuesday, May 5, 2020 9:49 AM
  • User-945256609 posted

    many thanks. I will modify the code as you had advised.

    Cheers

    Sathya

    Tuesday, May 5, 2020 11:44 AM