Asp.net mvc 5 NinjectWebCommon没有';t从其他组件加载模块
我正在尝试使用Ninject加载依赖项。但是NinjectWebCommon无法在引用的程序集中加载NinjectModuleAsp.net mvc 5 NinjectWebCommon没有';t从其他组件加载模块,asp.net-mvc-5,ninject.web.mvc,Asp.net Mvc 5,Ninject.web.mvc,我正在尝试使用Ninject加载依赖项。但是NinjectWebCommon无法在引用的程序集中加载NinjectModule using System; using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Common; public static class NinjectWebCommon { private sta
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();
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)
{
}
}
使用系统;
使用System.Web;
使用Microsoft.Web.Infrastructure.DynamicModuleHelper;
使用Ninject;
使用Ninject.Web.Common;
公共静态类NinjectWebCommon
{
私有静态只读引导程序Bootstrapper=new Bootstrapper();
///
///启动应用程序
///
公共静态void Start()
{
RegisterModule(typeof(OnePerRequestHttpModule));
RegisterModule(typeof(NinjectHttpModule));
初始化(CreateKernel);
}
///
///停止应用程序。
///
公共静态无效停止()
{
bootstrapper.ShutDown();
}
///
///创建将管理应用程序的内核。
///
///创建的内核。
私有静态IKernel CreateKernel()
{
var kernel=新的标准内核();
尝试
{
kernel.Bind().ToMethod(ctx=>()=>newbootstrapper().kernel);
kernel.Bind().To();
注册服务(内核);
返回内核;
}
抓住
{
Dispose();
投掷;
}
}
///
///在这里加载您的模块或注册您的服务!
///
///内核。
私有静态无效注册服务(IKernel内核)
{
}
}
下面是我正在尝试加载的模块
using System;
using Ninject.Modules;
namespace App.Security
{
public class SecurityModule : NinjectModule
{
public override void Load()
{
Bind<IAppAuthenticationManager>().To<AppAuthenticationManager>();
Bind(typeof(IAppUserManager<>)).To(typeof(AppUserManager<>));
Bind<AppUser>().ToSelf();
}
}
}
使用系统;
使用Ninject.Modules;
名称空间App.Security
{
公共类安全模块:NinjectModule
{
公共覆盖无效负载()
{
绑定()到();
绑定(typeof(IAppUserManager))。到(typeof(AppUserManager));
Bind().ToSelf();
}
}
}
我不明白为什么它不能加载模块?经过一番激烈的讨论后,我发现我需要打电话
kernel.Load<SecurityModule>();
kernel.Load();
在NinjectWebCommon的RegisterServices方法中。您所说的“NinjectWebCommon无法加载”是什么意思?你看到一条特殊的错误消息了吗?基本上我在另一个dll中有一个SecurityModule,比如说App.Security。现在,我在UI应用程序中安装了包Ninject.Mvc5。它添加了NinjectWebCommon。在这里,我期待着当调用create内核时,它会自动将NinjectModules添加到不同的引用dll中,以加载到Kernel.Cool中。谢谢分享。请
接受此答案(您也可以接受自己的答案),以便其他人清楚问题状态