asp.net-web-api – 具有Web Api RC的Ninject InSingletonScope

前端之家收集整理的这篇文章主要介绍了asp.net-web-api – 具有Web Api RC的Ninject InSingletonScope前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Ninject的InSingletonScope绑定与Web Api RC有一些困难。无论我如何创建我的绑定,它似乎也许Web Api正在处理范围/生命周期而不是Ninject。

我已经尝试了几个变体,用于连接Ninject。最常见的是与这里的答案相同:
ASP.NET Web API binding with ninject

我也试过这个版本:
http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/

在这两个方面,我真的是创建了一个开箱即用的Web Api项目,然后按照任一文章中的描述添加了Ninject软件包。最后,我添加了Resolver和Scope类,如StackOverflow版本:

public class NinjectDependencyScope : IDependencyScope
{
    private IResolutionRoot resolver;

    internal NinjectDependencyScope(IResolutionRoot resolver)
    {
        Contract.Assert(resolver != null);

        this.resolver = resolver;
    }

    public void Dispose()
    {
        IDisposable disposable = resolver as IDisposable;
        if (disposable != null)
            disposable.Dispose();

        resolver = null;
    }
    public object GetService(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this","This scope has already been disposed");
        return resolver.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this","This scope has already been disposed");
        return resolver.GetAll(serviceType);
    }
}

和:

public class NinjectDependencyResolver : NinjectDependencyScope,IDependencyResolver
{
    private IKernel kernel;

    public NinjectDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        this.kernel = kernel;
    }
    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(kernel.BeginBlock());
    }
}

然后,NinjectWebCommon看起来像这样:

using System.Web.Http;
using MvcApplication2.Controllers;

[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication2.App_Start.NinjectWebCommon),"Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication2.App_Start.NinjectWebCommon),"Stop")]

namespace MvcApplication2.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

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

            // Register Dependencies
            RegisterServices(kernel);

            // Set Web API Resolver
            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(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)
        {
            kernel.Bind<ILogger>().To<Logger>().InSingletonScope();
        }
    }
}

ILogger和Logger对象不做任何事情,但说明问题。 Logger做Debug.Writeline,以便我可以看到它被实例化。每页刷新页面都会显示每次呼叫正在刷新,而不是我希望的单身人士。这是一个使用Logger的控制器:

public class ValuesController : ApiController
{
    private readonly ILogger _logger;
    public ValuesController(ILogger logger)
    {
        _logger = logger;
        _logger.Log("Logger created at " + System.DateTime.Now.ToLongTimeString());
    }
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1","value2" };
    }
    // GET api/values/5
    public string Get(int id)
    {
        return "value";
    }
    // POST api/values
    public void Post(string value)
    {
    }
    // PUT api/values/5
    public void Put(int id,string value)
    {
    }
    // DELETE api/values/5
    public void Delete(int id)
    {
    }
}

当我将跟踪信息放入内核的创建中时,似乎表明内核只创建一次。那么…我看不到什么为什么单身人士不存在?

解决方法

使用
public IDependencyScope BeginScope()
{
    return new NinjectDependencyScope(kernel);
}

并且不要在NinjectDependencyScope中配置内核

猜你在找的asp.Net相关文章