4

我有一个 MVC4 Web 应用程序项目作为更大解决方案的一部分。我也有一个测试项目。我正在处理一堆不会重新编写的代码,所以我不能总是做出我想做的改变。

MVC4 Web 应用程序具有“普通”控制器和 Web API 控制器。我们使用的是 RTM 版本的 Web API,而不是 RC。

我试图将 IoC 引入项目中。使用 NuGet 安装技术(而不是下载 DLL 并直接引用它们),我安装了:

Ninject v3.0.1.10, 
Ninject.MVC3 v3.0.0.6
Ninject.Extensions.Factory v3.0.1.0
Ninject.Web.Common v 3.0.0.7

我的解决方案中没有其他使用 Ninject 的引用组件。

然后,按照 Brad Wilson 和他的 Github Gist https://gist.github.com/2417226给出的建议,以及 Filip W 在此处给出的类似建议http://www.strathweb.com/2012/05/using- ninject-with-the-latest-asp-net-web-api-source/,我已经实现了一个 NinjectResolver,并在全局配置中“注册”。

当我启动 Web 应用程序时,默认页面映射IndexProjectController. 这会呈现一个视图,该视图使用 Knockout 通过调用ApiController名为 的操作来填充 ViewModel ApiProjectController.Get()

我的NinjectWebCommon.cs代码如下所示:

using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Web.Http;
using System.Web.Http.Dependencies;
using Ninject.Extensions.Factory;
using Ninject.Syntax;
using OfficeWebApp.Utilities;

[assembly: WebActivator.PreApplicationStartMethod(typeof(OfficeWebApp.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(OfficeWebApp.App_Start.NinjectWebCommon), "Stop")]

namespace OfficeWebApp.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

    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();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);

            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IDataManagerConnection>().To<DataManagerConnection>().WithConstructorArgument("overriddenConnectionString", string.Empty);
            kernel.Bind<IDataManagerConnectionFactory>().ToFactory();
        }        
    }

    public class NinjectDependencyScope : IDependencyScope
    {
        private IResolutionRoot resolver;

        internal NinjectDependencyScope(IResolutionRoot resolver)
        {
            Contract.Assert(resolver != null);

            this.resolver = resolver;
        }

        public void Dispose()
        {
            IDisposable disposable = resolver as IDisposable;
            if (disposable != null)
                disposable.Dispose();

            resolver = null;
        }

        public object GetService(Type serviceType)
        {
            if (resolver == null)
                throw new ObjectDisposedException("this", "This scope has already been disposed");

            return resolver.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (resolver == null)
                throw new ObjectDisposedException("this", "This scope has already been disposed");

            return resolver.GetAll(serviceType);
        }
    }

    public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
    {
        private IKernel kernel;

        public NinjectDependencyResolver(IKernel kernel)
            : base(kernel)
        {
            this.kernel = kernel;
        }

        public IDependencyScope BeginScope()
        {
            return new NinjectDependencyScope(kernel.BeginBlock());
        }
    }
}

ProjectController代码是:

public class ProjectController : Controller
{
    private readonly IDataManagerConnectionFactory _dataManagerConnectionFactory;

    public ProjectController(IDataManagerConnectionFactory dataManagerConnectionFactory)
    {
        _dataManagerConnectionFactory = dataManagerConnectionFactory;
    }

    [HttpGet]
    public ActionResult Index()
    {
        //TODO:             
        ViewBag.Organisation = "Preview";

        return View();
    }
}

...和ApiProjectController

public class ApiProjectController : ApiController
{
    private readonly IDataManagerConnectionFactory _dataManagerConnectionFactory;

    public ProjectsController(IDataManagerConnectionFactory dataManagerConnectionFactory)
    {
        _dataManagerConnectionFactory = dataManagerConnectionFactory;
    }

    [HttpGet]
    public IEnumerable<ProjectTileModel> Get()
    {
        using (IDataManagerConnection connection = _dataManagerConnectionFactory.Create())
        {
            List<ProjectTileModel> projectViewModels = connection.DataManager.GetProjectInfos()
                                                                             .ToList();
            return projectViewModels;
        }
    }
}

ApiProjectController.Get()action 方法完成后,Ninject 向我抛出以下异常:

Error loading Ninject component ICache
No such component has been registered in the kernel's component container.

Suggestions:
  1) If you have created a custom subclass for KernelBase, ensure that you have properly
     implemented the AddComponents() method.
  2) Ensure that you have not removed the component from the container via a call to RemoveAll().
  3) Ensure you have not accidentally created more than one kernel.

调用堆栈如下所示:

Ninject.dll!Ninject.Components.ComponentContainer.Get(System.Type component) Line 160   C#
Ninject.dll!Ninject.Components.ComponentContainer.Get<Ninject.Activation.Caching.ICache>() Line 116 + 0x46 bytes    C#
Ninject.Web.Common.dll!Ninject.Web.Common.OnePerRequestHttpModule.DeactivateInstancesForCurrentHttpRequest.AnonymousMethod__1(Ninject.IKernel kernel) Line 74 + 0x27 bytes  C#
Ninject.dll!Ninject.GlobalKernelRegistration.MapKernels(System.Action<Ninject.IKernel> action) Line 75 + 0xe bytes  C#
Ninject.Web.Common.dll!Ninject.Web.Common.OnePerRequestHttpModule.DeactivateInstancesForCurrentHttpRequest() Line 76    C#
Ninject.Web.Common.dll!Ninject.Web.Common.OnePerRequestHttpModule.Init.AnonymousMethod__0(object o, System.EventArgs e) Line 56 + 0x9 bytes C#

在文件中的以下 Ninject 代码中引发了此异常ComponentContainer.cs

        Type implementation = _mappings[component].FirstOrDefault(); // <-- see note below...

        if (implementation == null)
            throw new InvalidOperationException(ExceptionFormatter.NoSuchComponentRegistered(component)); // <-- exception thrown here

注意:在上面指出的行中,该_mappings集合仅包含一项;键与Type我们正在寻找的 ( ICache) 匹配,但Values成员 (即 a List<Type>) 为空 (0 计数)

应该使用OnePerRequestHttpModule吗?是否因为我.ToFactory()在绑定中使用而发生了一些有趣的事情?我真的不知道为什么OnePerRequestHttpModule调用DeactivateInstancesForCurrentHttpRequest()但 Ninject 似乎想要获取它的内部缓存(也许??)

我做错了什么?

4

1 回答 1

0

我从来没有真正深入了解这一点。我不知道这是否是 Ninject 中的错误,或者我是否只是错误地使用了它。但是,我通过将 IoC 容器切换到 AutoFAC 来解决我的问题。

于 2012-12-19T22:50:32.823 回答