Access to Ninject Kernel.Get () from HttpHandler with existing user base

I have an ASP.NET ASP.NET application that uses Ninject 2.2.0.0

I have an HTTPHandler that inherits from the Microsoft.Web.ImageHandler class.

Inside it, I need to access an instance of the service class that I created.

because I cannot inherit from Ninject.Web.HttpHandlerBase, I thought I would just open the kernel as a property of the Global.asax class ...

protected override IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel(new DefaultModule());

        var sms = kernel.Get<SiteMapService>();
        SiteMapSvc = sms;
        Kernel = kernel;
        return kernel;

    }

    public IKernel Kernel
    {
        get; set;
    }

and use the kernel.Get method to get the service.

var global = (Global) HttpContext.Current.ApplicationInstance;

var service = global.Kernel.Get<PhotoService>();

This does not work with the following ...

   [ArgumentNullException: Cannot be null
Parameter name: root]
   Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:258
   Ninject.ResolutionExtensions.Get(IResolutionRoot root, Type service, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:151
   Thumb.GenerateImage(NameValueCollection parameters) in \Thumb.ashx.cs:40

UPDATE: I was able to fix this by changing the Global.Kernel property for this, but now I am in the territory of the anti-template ...

public IKernel Kernel
    {
        get { return this.CreateKernel(); }
    }

Now read and see what that means.

+2
1

- Service-Locator. , IoC , .

, "KernelContainer.Inject(this)" HttpHandler. ( Ninject.Web) .

+6

All Articles