17

Ninject の InSingletonScope バインディングを Web Api RC で使用するのに問題があります。バインディングをどのように作成しても、おそらく Web Api が Ninject ではなくスコープ/ライフタイムを処理しているように見えます。

Ninject の配線にいくつかのバリエーションを試しました。最も一般的なのは、次の回答と同じです: ninject を使用した ASP.NET Web API バインディング

このバージョンも試しました: http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/

どちらの場合も、文字通りすぐに使用できる Web Api プロジェクトを作成し、いずれかの投稿で説明されているように Ninject パッケージを追加しています。最後に、StackOverflow バージョンの場合のように、Resolver クラスと Scope クラスを追加します。

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 を実行して、いつインスタンス化されたかを確認できるようにします。そして、ページを更新するたびに、私が望んでいたシングルトンではなく、呼び出しごとに更新されていることが示されます。ロガーを使用したコントローラーは次のとおりです。

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

カーネルの作成にトレース情報を入れると、カーネルが 1 回しか作成されていないように見えます。それで... 何が見えないのですか?シングルトンが永続化されないのはなぜですか?

4

3 に答える 3

41

使用する

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

カーネルをNinjectDependencyScope

于 2012-07-06T07:36:07.873 に答える
0

@Remo Gloor WebAPIのInMemoryHostでコードを実行し、統合テストを実行すると、すべてが正常に機能し、シングルトンがあります。VS Cassini Webサーバー内でWebAPIソリューションを実行すると、最初の実行は成功し、[更新]をクリックすると例外が発生 します。NinjectコンポーネントICacheの読み込みエラーそのようなコンポーネントはカーネルのコンポーネントコンテナに登録されていません。

BeginBlockで古いコードを返すと、カッシーニでは機能しますが、統合テストではIsSingletonが機能しなくなります。

于 2012-09-10T13:12:45.973 に答える