NinjectWebCommon 不从其他程序集加载 NinjectModule
NinjectWebCommon doesn't load NinjectModule from other assemblies
我正在尝试使用 Ninject 加载依赖项。但是 NinjectWebCommon 无法在引用的程序集中加载 Ninject 模块。
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)
{
}
}
下面是我要加载的模块
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();
}
}
}
我无法弄清楚为什么它无法加载模块?
经过一些头脑风暴之后,我发现我需要打电话给
kernel.Load<SecurityModule>();
在 NinjectWebCommon 的 RegisterServices 方法中。
我正在尝试使用 Ninject 加载依赖项。但是 NinjectWebCommon 无法在引用的程序集中加载 Ninject 模块。
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)
{
}
}
下面是我要加载的模块
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();
}
}
}
我无法弄清楚为什么它无法加载模块?
经过一些头脑风暴之后,我发现我需要打电话给
kernel.Load<SecurityModule>();
在 NinjectWebCommon 的 RegisterServices 方法中。