您是否使用Nuget添加Ninject?您需要參考WebActivatorEx
以使引導程序正常工作(顯然與其他必需的Ninject引用一起)。在您項目的App_Start
文件夾中添加一個NinjectWebCommon.cs類,如下所示:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourMvcApp.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(YourMvcApp.App_Start.NinjectWebCommon), "Stop")]
namespace YourMvcApp.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(); // you'll add modules to the parameter list here
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)
//{
//}
}
}
來源
2014-05-05 03:07:38
bcr
您面臨的確切問題是什麼?可以詳細說明 – qamar
@qamar am遵循這個http://www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#book-unique-identifier – tmcpete
你可以在你的global.asax app_start中創建一個ninjectwebcommon類的實例功能。它會被實例化,當應用程序啓動 – qamar