How to get GlassMapper work with Ninject

107 views
Skip to first unread message

Dirk Schäfauer

unread,
Aug 20, 2014, 10:24:37 AM8/20/14
to glass...@googlegroups.com
Hi,

I currently try to get GlassMapp work with Ninject DI. But I get an error:

Server Error in '/' Application. 

The given key was not specified in the dictionary.

My NinjectWebCommon in App_Start looks like this:

using System;
using System.Web;
using Glass.Mapper.Sc;
using Glass.Mapper.Sc.CastleWindsor;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Syntax;
using Ninject.Web.Common;
using SeitenKern;
using SeitenKern.Infrastructure.DependencyInjection.Modules;
using Config = Glass.Mapper.Sc.CastleWindsor.Config;


[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")]


namespace SeitenKern
{
   
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(new SeitenKernModule());
           
try
           
{
                kernel
.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel
.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();


               
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 the module itself looks like this:

using Glass.Mapper.Sc;
using Ninject.Modules;


namespace SeitenKern.Infrastructure.DependencyInjection.Modules
{
   
public class SeitenKernModule : NinjectModule
   
{
       
public override void Load()
       
{
           
Bind<ISitecoreContext>().To<SitecoreContext>();
       
}
   
}
}


I try to use the SitecoreContext in my MasterPage:

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Glass.Mapper.Sc;
using Ninject;
using Ninject.Web.Common;
using SeitenKern.Infrastructure.Controls;
using SeitenKern.Models;
using SeitenKern.SitecoreContrib;
using Sitecore.Data;
using Sitecore.Links;
using Sitecore.Platform;
using Sitecore.Web.UI.WebControls;


namespace SeitenKern.layouts.SeitenKern
{
   
public partial class SeitenKernBase : BasePage
   
{
       
[Inject]
       
public ISitecoreContext SitecoreContext { get; set; }


       
public HomePage HomePageModel { get; set; }


       
protected override void OnLoad(EventArgs e)
       
{
           
base.OnLoad(e);


           
var kernel = new Bootstrapper().Kernel;
            kernel
.Inject(this);


           
HomePageModel = SitecoreContext.GetCurrentItem<HomePage>();
       
}
   
}
}


Could anyone advice what I am doing wrong?

I tested it with a custom Interface and Implementation and everything works fine, so module is loaded as expected. I'm also not able to debug the error.

Regards Dirk
Reply all
Reply to author
Forward
0 new messages