Mặc dù There is nothing special about App_Start, nhưng bạn có thể làm cho các file insinde thư mục này thực hiện khi ứng dụng bắt đầu như Application_Start
bên Global.asax
. Tôi đang sử dụng Ninject injency injector trong dự án của tôi trong đó có App_Start
thư mục. Có không có tập tin Global.asax trong dự án của tôi:
nhưng tất cả cấu hình tôi đã thiết lập trong NinjectWebCommon
tập tin sẽ được thực hiện khi bắt đầu ứng dụng. NinjectWebCommon
có nội dung sau:
using WebFormNinject.Models;
[assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")]
namespace WebFormNinject.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);
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<IDisplay>().To<MockDisplay>();
}
}
}
Tôi đã tò mò nơi chức năng RegisterServices
sẽ được thực thi! Sau đó, tôi nhận thấy phần này của mã:
[assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")]
Các thuộc tính này làm cho phương thức Start
được thực hiện khi bắt đầu ứng dụng. Để biết thêm thông tin, hãy xem WebActivator/PreApplicationStartMethod
Nguồn
2014-02-07 10:40:12
Tất cả các lớp đó được tham chiếu trong Global.asax ở đâu? Một số tham chiếu phải ở đâu đó, chúng không được tham chiếu trực tiếp. – FrenkyB