0

Hi I am working on a project that needs a separationg beetween an mvc project and a web api project and I created both of them.

Only my web api project requires a dependency injection framework so I have installed Ninject.WebApi.DependencyResolver.

This is the code that came with it:

  public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();


    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        return kernel;
    }

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Load(Assembly.GetExecutingAssembly());
        kernel.Load(Assembly.GetExecutingAssembly());
        kernel.Bind<IUserRepository>().To<UserRepository>();
        kernel.Bind<RepositoryFactories>().To<RepositoryFactories>().InSingletonScope();
        kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>().InSingletonScope();
        kernel.Bind<IEConnectUow>().To<EConnectUow>();
        kernel.Bind<IUserService>().To<SessionUserService>();
        kernel.Bind<IValidationService>().To<ValidationService>();
    }        
}

In order for everything to be initialized I have called NinjectWebApi.Start() in the Global.asx file of the mvc project.

When I try to start the application I get an InvalidOperationException on the first line of Start() method of the NinjectWebCommon class.

Is there something I am missing here?

aleczandru
  • 5,319
  • 15
  • 62
  • 112

1 Answers1

1

This class is intended to run before the application is started using the WebActivator. If you want to start without WebActivator you have to register these Modules in the web.config. See the documentation.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98