1

I am trying to use Bootstrapper to do initialization for my application, ioc, automapper, configuration etc.

I need some direction on how to setup the ninject correctly in asp.net webapi using bootstrapper. With the following configuration, my apicontroller is not able to resolve the IMyService dependency. Looks like it's using different ninject kernal.

My NinjectWebCommon

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>();

        GlobalConfiguration.Configuration.DependencyResolver = new MyResolver(kernel);

        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)
    {
    }        
}

bootstrapper ninject registration

public class DIRegistration : INinjectRegistration
{
    public void Register(IKernel container)
    {
        container.Bind<IMyService>().To<MyService>().InTransientScope();
    }
}

public class MyService: IMyService
{
    public string GetString()
    {
        return "My String!!!!!";
    }
}

public interface IMyService 
{
    string GetString();
}
cvbarros
  • 1,684
  • 1
  • 12
  • 19
Eatdoku
  • 6,569
  • 13
  • 63
  • 98

3 Answers3

1

What i did to solve the problem is moving bootstrapper.Initialize(CreateKernel); to the IStartupTask implementation i have created for NinjectWebBootstrap. Basically use the IKernal injected by the Bootstrapper framework and run my registration and at the end set asp.net webapi and then set DepdendencyResolver with the custom resolve that takes in the injected IKernal instance.

Eatdoku
  • 6,569
  • 13
  • 63
  • 98
0

DIRegistration is never used. So that binding does not exist.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Bootstrapper picks up INinjectRegistration implementation and executes the Register method to do whatever you decided to do in there – Eatdoku Mar 21 '13 at 22:03
0

I was trying to update the values directly in database but values were not updated in my WCF which uses entity framework using Ninject in svc file - the same way as in https://github.com/ninject/ninject.extensions.wcf/tree/master/src/Examples/WcfTimeService and using bootstraping.

If found out that if I recycle the IIS app pool, the data gets refreshed and it works as expected. I guess it IIS and Ninject which is creating this caching issue. Do you have any idea about it?

Vikas
  • 31
  • 4