1

I am Using MVC6- WebApi Parameterized constructor not calling. I referenced Ninject the below reference.

http://sudiptachaudhari.com/dependency-injection-aspnet-webapi/

WebApi Controller

 public class EmployeeController : ApiController
    {
        private IEmployeeDetailRepository _IEmployeeDetailRepository;
        public EmployeeController() { }
        public EmployeeController(IEmployeeDetailRepository IEmployeeDetailRepository)
        {
            _IEmployeeDetailRepository = IEmployeeDetailRepository;
        }
        public JsonResult<List<EmployeeDetailDTO>> GetAllEmployees()
        {
            var list = _IEmployeeDetailRepository.EmployeeList().ToList();
            return Json<List<EmployeeDetailDTO>>(list);
        }
    }

Not calling below parameterized construction

public EmployeeController(IEmployeeDetailRepository IEmployeeDetailRepository)

I don't understand what is wrong.

In the NinjectResolver class under the App_Start

public class NinjectResolver : IDependencyResolver
    {
        private IKernel kernel;

        public NinjectResolver() : this(new StandardKernel())
        {
        }
        public NinjectResolver(IKernel ninjectKernel, bool scope = false)
        {
            kernel = ninjectKernel;
            if (!scope)
            {
                AddBindings(kernel);
            }
        }

        private void AddBindings(IKernel kernel)
        {
            // singleton and transient bindings go here
        }

        private IKernel AddRequestBindings(IKernel kernel)
        {
            kernel.Bind<IEmployeeDetailRepository>().To<EmployeeDetailRepository>().InSingletonScope();
            return kernel;
        }

        public IDependencyScope BeginScope()
        {
            return new NinjectResolver(AddRequestBindings(new ChildKernel(kernel)), true);
        }

        public void Dispose()
        {
        }

        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }

WebApi Configuration Class

 [ExcludeFromCodeCoverage]
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

           config.DependencyResolver = new NinjectResolver();
            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }

    }

Global.asax class

 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            Mapper.Initialize(cfg => cfg.AddProfile<AutoMapperEmployeeProfile>());
        }
2
  • 1
    Remove the default parameter less constructor from the api controller Commented Jul 30, 2019 at 9:39
  • @Nkosi, I have removed and tried but still same issue. Commented Oct 9, 2019 at 10:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.