[assembly: WebActivator.PreApplicationStartMethod(typeof(BusinessLogic.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(BusinessLogic.App_Start.NinjectWebCommon), "Stop")]
namespace
BusinessLogic.App_Start
{
using System;
using System.Web;
using Data;
using Data.Contracts;
using Data.Helpers;
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 an instance of IKernal
/// </summary>
public static IKernel Kernel { get; private set; }
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
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<RepositoryFactories>().To<RepositoryFactories>().InSingletonScope();
kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
}
[Inject]
public IUnitOfWork Uow { get; set; }
public EMPOS GetPOSByDate(string EmpId, DateTime FindDate)
{
EMPOS pos = (from p in Uow.EMPOSs.GetAll()
where p.DETNUMBERA == EmpId &&
p.POSSTARTC <= FindDate &&
(p.POSENDD >= FindDate ||
p.POSENDD == null)
select p).Single();
return pos;
}
using System;
using System.Web.UI;
using Data.Contracts;
using Ninject;
namespace ConsoleApplication1
{
public abstract class BasePage : Ninject.Web.PageBase
{
// NOT NECESSARY TO DISPOSE THE UOW IN OUR CONTROLLERS
// Recall that we let IoC inject the Uow into our controllers
// We can depend upon on IoC to dispose the UoW for us
[Inject]
public IUnitOfWork Uow { get; set; }
}
}
[assembly: WebActivator.PreApplicationStartMethod(typeof(ConsoleApplication1.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(ConsoleApplication1.App_Start.NinjectWebCommon), "Stop")]
namespace
ConsoleApplication1.App_Start
{
using System;
using System.Web;
using Data;
using Data.Contracts;
using Data.Helpers;
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<RepositoryFactories>().To<RepositoryFactories>().InSingletonScope();
kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
}
As I said this all works fine, but when I try to do this in my web forms app I get an error:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.Contracts;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using Ninject;
using BusinessLogic;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
var BL = new BusinessLayer();
var kernel = new StandardKernel();
kernel.Bind<Data.Helpers.RepositoryFactories>().To<Data.Helpers.RepositoryFactories>().InSingletonScope();
kernel.Bind<Data.Helpers.IRepositoryProvider>().To<Data.Helpers.RepositoryProvider>();
kernel.Bind<IUnitOfWork>().To<Data.UnitOfWork>().InSingletonScope();
kernel.Bind<Leave>().To<Leave>().InTransientScope();
kernel.Inject(BL);
while (true)
{
try
{
Console.WriteLine("Enter an employee number...");
string emp = Console.ReadLine();
var cont = BL.GetContinualServiceDate(emp);
if (cont == null)
throw new Exception("The date returned was null, the employee may be a leaver or may not exist.");
Console.WriteLine("Continual Service Start Date : " + (DateTime)cont);
Leave lv = kernel.Get<Leave>();
lv.FindLeave(emp);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
public class Leave : BasePage
{
public void FindLeave(string emp)
{
IEnumerable<EMLVE> abc = Uow.EMLVEs.GetAll().Where(a => a.DETNUMBERA == emp).ToList();
foreach (EMLVE a in abc)
{
Console.WriteLine(a.DETNUMBERA + " : " + a.LMDSTARTC + " : " + a.LMDTYPECDA);
}
}
}
}
|
|
[assembly: WebActivator.PreApplicationStartMethod(typeof(Dash.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(Dash.App_Start.NinjectWebCommon), "Stop")]
namespace
Dash.App_Start
{
using System;
using System.Web;
using Data;
using Data.Contracts;
using Data.Helpers;
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 an instance of IKernal
/// </summary>
public static IKernel Kernel { get; private set; }
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
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<RepositoryFactories>().To<RepositoryFactories>().InSingletonScope();
kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
}
[WebMethod]
public static AutoCompleteBoxData GetResults(RadAutoCompleteContext context)
{
var BL = new BusinessLayer();
Dash.App_Start.NinjectWebCommon.Kernel.Inject(BL); //now the Uow field is injected
string searchString = ((Dictionary<string, object>)context)["Text"].ToString();
List<AutoCompleteBoxItemData> staff =
(from s in BL.MyStaffSearch()
where s.Text.Contains(searchString)
select s).ToList();
AutoCompleteBoxData res = new AutoCompleteBoxData();
res.Items = staff.ToArray();
return res;
}