57 lines
1.9 KiB
C#
57 lines
1.9 KiB
C#
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
|
|
using Ninject;
|
|
using Ninject.Activation;
|
|
using Ninject.Modules;
|
|
using Ninject.Web.Common;
|
|
using Ninject.Web.Common.WebHost;
|
|
using PartSource.Entities;
|
|
using PartSource.Entities.Nexpart;
|
|
using PartSource.Services;
|
|
using System;
|
|
using System.Web;
|
|
|
|
namespace PartSource
|
|
{
|
|
public static class NinjectWebCommon
|
|
{
|
|
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
|
|
|
|
public static void Start()
|
|
{
|
|
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
|
|
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
|
|
NinjectWebCommon.bootstrapper.Initialize(new Func<IKernel>(NinjectWebCommon.CreateKernel));
|
|
}
|
|
|
|
public static void Stop()
|
|
{
|
|
NinjectWebCommon.bootstrapper.ShutDown();
|
|
}
|
|
|
|
private static IKernel CreateKernel()
|
|
{
|
|
StandardKernel standardKernel = new StandardKernel(Array.Empty<INinjectModule>());
|
|
try
|
|
{
|
|
standardKernel.Bind<Func<IKernel>>().ToMethod((Func<IContext, Func<IKernel>>)(ctx => (Func<IKernel>)(() => new Bootstrapper().Kernel)));
|
|
standardKernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
|
|
NinjectWebCommon.RegisterServices((IKernel)standardKernel);
|
|
return (IKernel)standardKernel;
|
|
}
|
|
catch
|
|
{
|
|
standardKernel.Dispose();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private static void RegisterServices(IKernel kernel)
|
|
{
|
|
kernel.Bind<PartSourceContext>().To<PartSourceContext>();
|
|
kernel.Bind<SecurityService>().To<SecurityService>();
|
|
kernel.Bind<NexpartService>().To<NexpartService>();
|
|
kernel.Bind<PSRequestHeader>().To<PSRequestHeader>();
|
|
}
|
|
}
|
|
}
|