Not Able to call the service layer in controller in mvc

Exception:

Nullreference exception

Here is my Code: Controller:

private IUserService UserService;
        public HomeController()
        { 
        }
        public HomeController(IUserService UserService)
        {
            this.UserService = UserService;
        }
    [HttpPost]
        public ActionResult Register(RegisterViewModel reg)
        {
            SADM_Users users = new SADM_Users();
            if (reg == null)
            {
                return Json(false);
            }
            else
            {
                FillUserMaster(users, reg);
                UserService.insertUser(users);

                ViewBag.ErrorMsg = "Succesfully added";
            }

            return View();
        }


Service Layer:

  public interface IUserService
    {

        void insertUser(SADM_Users users);
    }
    public class UserService:IUserService
    {
        private readonly ILoginRepository LoginRepository;
        private readonly IUnitOfWork unitOfWork;

        public UserService(ILoginRepository LoginRepository)
        {
            this.LoginRepository = LoginRepository;
        }
        public void insertUser(SADM_Users users)
        {
            try
            {
                LoginRepository.Add(users);
                unitOfWork.Commit();
            }
            finally {
                users = null;
            }
        }
    }

I'm creating an mvc app with a service layer. I'm used to call services using in my controllers, but these services have not been Called. please some one help on this.and i wnt to know any dependancy factor.

Upvotes: 0

Views: 45

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

Remove the default constructor from your HomeController and make sure that you are using a DI library that will inject the proper implementation of IUserService into it.

Upvotes: 1

Related Questions