I do it Manualy and it work in my project.
for using this approach you should follow below steps:
1.install ninject package via nuget package manager
2.create new class in app_start folder and set the name of class to NinjectWebCommon.cs for and then replace the content of class with below code:
> [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(projectName.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(projectName.App_Start.NinjectWebCommon), "Stop")]
namespace projectName.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using MultiLanguage;
using Ninject;
using Ninject.Web.Common;
using Ninject.Web.Common.WebHost;
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>();
kernel.Bind<Iemployee>().To<Employee>();
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)
{
}
}
}
and as last step you should solve the namespace of your class
for doing this press ctrl+.
and add the dll that i attatched to this post
[enter link description here][1]
[1]: http://s8.picofile.com/file/8320685526/WebActivatorEx.dll.html
Although [There is nothing special about App_Start][1], but you can make files insinde this folder executes when application started like `Application_Start` inside `Global.asax`. I am using [Ninject][2] dependency injector in my project which has `App_Start` folder. There is **no Global.asax** file in my project:
![enter image description here][3]
but all configuration i have set in `NinjectWebCommon` file will be executed upon starting application. `NinjectWebCommon` has following content:
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>();
}
}
}
I was curious where `RegisterServices` function will be executed! Then I noticed to this section of code:
[assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")]
These attributes make `Start` method executed on application started. For more information look at [WebActivator / PreApplicationStartMethod][4]
[1]: https://stackoverflow.com/questions/14228072/app-start-folder-in-asp-4-5-only-in-webapplications-projects#answer-14228455
[2]: http://www.ninject.org/
[3]: http://i.stack.imgur.com/EuMVP.jpg
[4]: http://ilearnable.net/2010/11/22/webactivator-preapplicationstartmethod/