使用Ninject

时间:2016-03-07 00:55:07

标签: c# dependency-injection ninject

以下是手头的问题:

通过CustomerController呼叫我的URL时,我收到以下异常:

  

ExceptionMessage:

     

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

我正在使用以下网址:

  

请注意:在我将逻辑重构为业务类并实现依赖注入之前,/api/Customer/调用正在进行。

我的研究表明,我没有正确地使用Ninject注册我的界面和课程,但不确定我缺少哪一步。

研究链接:

  

以下是我的问题导致此异常的原因是什么?我正在Ninject内注册我的界面/类,但它似乎没有正确识别映射。有什么想法吗?

客户控制器

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>();
        }        
    }
}

1 个答案:

答案 0 :(得分:6)

对于同样的问题,我安装了nuget包

  • ninject
  • ninject.web.common
  • ninject.web.common.webhost
  • ninject.web.webapi
  • <强> ninject.web.webapi.webhost

    并且工作