本文介绍了使用Ninject确保控制器具有无参数的公共构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是眼前的问题:

通过URL调用我的CustomerController时,出现以下异常:

While calling my CustomerController through the URL, I get the following exception:

尝试创建类型的控制器时发生错误 'CustomerController'.确保控制器有一个 无参数的公共构造函数.

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

我正在使用以下网址:

  • http://localhost:55555/api/Customer/
  • http://localhost:55555/api/Customer/8

我的研究表明,我没有在Ninject中正确注册我的界面和类,但是不确定我缺少什么步骤.

My research suggests that I am not registering my interface and class correctly with Ninject, but not sure what step I am missing.

研究链接:

  • Make sure that the controller has a parameterless public constructor error
  • Make sure that the controller has a parameterless public constructor in Unity

客户总监

public class CustomerController : ApiController
{
    private readonly ICustomerBusiness _customerBusiness;

    public CustomerController(ICustomerBusiness customerBusiness)
    {
        _customerBusiness = customerBusiness;
    }

    // GET api/Customer
    [HttpGet]
    public IEnumerable<Customer> GetCustomers()
    {
        return _customerBusiness.GetCustomers();
    }

    // GET api/Customer/Id
    [HttpGet]
    public IEnumerable<Customer> GetCustomersById(int customerId)
    {
        return _customerBusiness.GetCustomerById(customerId);
    }
}

客户业务

public class CustomerBusiness : ICustomerBusiness
{
    private readonly DatabaseContext _databaseContext = new DatabaseContext();

    public IEnumerable<Customer> GetCustomers()
    {
        return _databaseContext.Customers;
    }

    public IQueryable<Customer> GetCustomerById(int customerId)
    {
        return _databaseContext.Customers.Where(c => c.CustomerId == customerId);
    }
}

客户业务界面

public interface ICustomerBusiness
{
    IQueryable<Customer> GetCustomerById(int customerId);
    IEnumerable<Customer> GetCustomers();
}

NinjectWebCommon

using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using MyReservation.API;
using MyReservation.API.Business;
using Ninject;
using Ninject.Web.Common;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")]

namespace MyReservation.API
{
    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<ICustomerBusiness>().To<CustomerBusiness>();
        }
    }
}

推荐答案

对于同一问题,我安装了nuget软件包

For the same problem I installed the nuget packages

  • 注入
  • ninject.web.common
  • ninject.web.common.webhost
  • ninject.web.webapi
  • ninject.web.webapi.webhost

  • ninject
  • ninject.web.common
  • ninject.web.common.webhost
  • ninject.web.webapi
  • ninject.web.webapi.webhost

工作

这篇关于使用Ninject确保控制器具有无参数的公共构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:13