Ninject InSingletonScope for asp.net mvc 4

时间:2012-10-18 23:40:46

标签: ninject

我正在尝试将InSingletonScope用于我的一个服务界面。但是,它仍在为每个Web请求创建一个新对象实例。基本上它的行为类似于我的asp.net mvc 4应用程序中的InRequestScope。

我认为InSingletonScope适用于IIS进程的生命周期?

我在我的一个NinjectModule中为接口注册了以下实现。如果我马上解决它,repo1和repo2实际上是同一个实例。但是,在我的控制器中,每个请求都会产生一个新实例。

--------------------模块注册

public class RepositoryModule : NinjectModule
{
    #region Overrides of NinjectModule

    public override void Load()
    {
        Bind<IFakeRepository>().To<FakeRepository>().InSingletonScope();

        // following code onle execute the constructor once
        var repo1String = Kernel.Get<IFakeRepository>().GetString();
        var repo2String = Kernel.Get<IFakeRepository>().GetString();
    }

    #endregion
}

--------------------存储库接口和实现

public interface IFakeRepository
{
    string GetString();
}

public class FakeRepository : IFakeRepository
{
    public FakeRepository()
    {
        // every web request execute this constructor
        Debug.Write("FakeRepository constructor called");
    }

    #region Implementation of IFackRepository

    public string GetString()
    {
        return "dummy string";
    }

    #endregion
}

------------------ web api controller

public class TestRepoController : ApiController
{
    public IFakeRepository FakeRepository { get; set; }

    public TestRepoController(IFakeRepository fakeRepository)
    {
        FakeRepository = fakeRepository;
    }

    public string Get()
    {
        return FakeRepository.GetString();
    }
}

----------------- web api route registration

config.Routes.MapHttpRoute(
         name: "TestTakeRoutePost",
         routeTemplate: "Fake",
         defaults: new { controller = "TestRepo" }
        );

----------------- NinjectWebCommon

[assembly: WebActivator.PreApplicationStartMethod(typeof(PNI.MediaServer.Application.App_Start.NinjectWebCommon), "Start")]

[assembly:WebActivator.ApplicationShutdownMethodAttribute(typeof(PNI.MediaServer.Application.App_Start.NinjectWebCommon),&#34; Stop&#34;)]

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

        //load all Binds defined in the classes that inherit NinhectModule
        kernel.Load(Assembly.GetExecutingAssembly());

        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        // Set Web API Resolver
        GlobalConfiguration.Configuration.DependencyResolver = new PniNinjectDependencyResolver(kernel);

        return kernel;
    }
}

0 个答案:

没有答案