0

I have created a asp.net web api project and implemented the below HTTP GET method in AccountController and the related service method & repository method in AccountService & AccountRepository respectively.

// WEB API 
public class AccountController : ApiController
{
    private readonly IAccountService _accountService;

    public AccountController(IAccountService accountService)
    {
        _accountService = accountService;
    }

    [HttpGet, ActionName("UserProfile")]
    public JsonResult<decimal> GetUserSalary(int userID)
    {
        var account = _accountService.GetUserSalary(userID);
        if (account != null)
        {
            return Json(account.Salary);
        }
        return Json(0);
    }
}

Service / Business Layer

public interface IAccountService
{
    decimal GetUserSalary(int userId);
}

public class AccountService : IAccountService
{
    readonly IAccountRepository _accountRepository = new AccountRepository();

    public decimal GetUserSalary(int userId)
    {
        return _accountRepository.GetUserSalary(userId);
    }
}

Repository / Data Access Layer

public interface IAccountRepository
{
    decimal GetUserSalary(int userId);
}

public class AccountRepository : IAccountRepository
{
    public decimal GetUserSalary(int userId)
    {
        using (var db = new AccountEntities())
        {
            var account = (from b in db.UserAccounts where b.UserID == userId select b).FirstOrDefault();
            if (account != null)
            {
                return account.Salary;
            }
        }
        return 0;
    }
}

UnityConfig

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
        container.RegisterType<IAccountService, AccountService>();
        container.RegisterType<IAccountRepository, AccountRepository>();
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}

But when I invoke the API method GetUserSalary() I get an error saying

An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.

Jasen
  • 14,030
  • 3
  • 51
  • 68

2 Answers2

2

Check that you did not forget to register Unity IoC container itself:

  • if you use ASP.NET Framework it could be - Global.asax or Startap.cs (Owin) via UnityConfig.RegisterComponents() method.
  • if you use ASP.NET Core then in the Startup.cs file (I was unable to find official guides for its configuting)
  • I have added UnityConfig.RegisterComponents() to Global.asax under Application_Start(). It worked. –  Nov 10 '18 at 04:12
-1

Your current constructor has parameters (or args if you prefer).

see:

public AccountController(IAccountService accountService)
{
    _accountService = accountService;
}

All you need to do is add a "Parameter-less Constructor" into the controller as well.

public AccountController()
{
}

Parameter-less constructors are usually above the ones that have params, though as far as I am aware this is only due to standards not any actual effect(s) it may cause.

There is also an already existing issue/question similar to this I will link below that may provide further details.

Make sure that the controller has a parameterless public constructor error

  • 2
    "All you need to do is add a "Parameter-less Constructor" into the controller as well." -> not if he depends on `IAccountService accountService`. He should check the DI registration – Peter Bons Nov 09 '18 at 19:06
  • @PeterBons How do we do that? Can you share a code? –  Nov 10 '18 at 04:03
  • I have added above code. But it returns null exception for _accountService. –  Nov 10 '18 at 04:04