[英]asp.net MVC 4 Ninject OnApplicationStarted()
Hello Im learning the basics of asp.net by making simple blog with this tutorial你好,我通过使用本教程制作简单的博客来学习 asp.net 的基础知识
http://www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#story1-configure-ninject-mvc http://www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#story1-configure-ninject-mvc
And when I want to configure Ninject for MVC project in MvcApplication.cs there is an error "'System.Web.HttpApplication' does not contain a definition for 'OnApplicationStarted'" what Im doing wrong?当我想在 MvcApplication.cs 中为 MVC 项目配置 Ninject 时出现错误“'System.Web.HttpApplication'不包含'OnApplicationStarted'的定义”我做错了什么? There is whole code for this:
有完整的代码:
using Ninject;
using Ninject.Web.Common;
using System.Web.Routing;
using System.Web.Mvc;
using JustBlog.Core;
namespace JustBlog
{
public class MvcApplication : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(new RepositoryModule());
kernel.Bind<IBlogRepository>().To<BlogRepository>();
return kernel;
}
protected override void OnApplicationStarted()
{
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
base.OnApplicationStarted();
}
}
}
I've got problem in the last line "base.OnApplicationStarted();".我在最后一行“base.OnApplicationStarted();”中遇到了问题。
Unfortunately, there's a lot of old information out there on Ninject... There is no reason to use the NinjectHttpApplication method anymore.不幸的是,有很多关于 Ninject 的旧信息......没有理由再使用 NinjectHttpApplication 方法了。
Instead, simply install NinjectMVCx where x is the version 3, 4 or 5. This adds a file to App_Start called NinjectWebCommon.cs that you can use to customize your bindings.相反,只需安装 NinjectMVCx,其中 x 是版本 3、4 或 5。这会向 App_Start 添加一个名为 NinjectWebCommon.cs 的文件,您可以使用它来自定义绑定。 There is no need for changing your HttpApplication or anything like that.
无需更改您的 HttpApplication 或类似内容。
You would put this code in RegisterServices of NinjectWebCommon.cs您可以将此代码放在 NinjectWebCommon.cs 的 RegisterServices 中
kernel.Load(new RepositoryModule());
kernel.Bind<IBlogRepository>().To<BlogRepository>();
Just use the file which was added by the package into App_Start: NinjectWebCommon.cs只需使用由包添加到 App_Start 中的文件: NinjectWebCommon.cs
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(SportStore.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(SportStore.App_Start.NinjectWebCommon), "Stop")]
namespace SportStore.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();
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.Mvc.DependencyResolver.SetResolver(new SportStore.Infrastructure.NinjectDependencyResolver(kernel));
}
}
} }
Add IDependencyResolver in my code it is in Infrastructure solution folder:在我的代码中添加 IDependencyResolver 它位于 Infrastructure 解决方案文件夹中:
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel iKernel;
public NinjectDependencyResolver(IKernel kernel)
{
iKernel = kernel;
AddBindigs();
}
public object GetService(Type serviceType)
{
return iKernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return iKernel.GetAll(serviceType);
}
private void AddBindigs()
{
//Add your bindings here....
iKernel.Bind<IProductRepository>().To<EFProductRepository>();
iKernel.Bind<IOrderProcessor>().To<EmailOrderProcessor>().
WithConstructorArgument("settings", emailSettings);
}
}
Application_Start:应用程序_开始:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.