Asp.net web api 带有Web Api RC的Ninject InSingletonScope
我在使用Ninject的InSingletonScope绑定与Web Api RC时遇到了一些困难。无论我如何创建绑定,看起来Web Api可能正在处理作用域/生存期而不是Ninject 我试过几种连接Ninject的方法。最常见的答案与这里的答案相同: 我也尝试过这个版本: 在这两篇文章中,我都创建了一个现成的WebAPI项目,然后添加了Ninject包,如两篇文章中所述。最后,我添加了解析器和作用域类,例如StackOverflow版本的:Asp.net web api 带有Web Api RC的Ninject InSingletonScope,asp.net-web-api,ninject,Asp.net Web Api,Ninject,我在使用Ninject的InSingletonScope绑定与Web Api RC时遇到了一些困难。无论我如何创建绑定,看起来Web Api可能正在处理作用域/生存期而不是Ninject 我试过几种连接Ninject的方法。最常见的答案与这里的答案相同: 我也尝试过这个版本: 在这两篇文章中,我都创建了一个现成的WebAPI项目,然后添加了Ninject包,如两篇文章中所述。最后,我添加了解析器和作用域类,例如StackOverflow版本的: public class NinjectDep
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);
}
}
然后,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();
}
}
}
使用System.Web.Http;
使用mvcapapplication2.控制器;
[程序集:WebActivator.PreApplicationStartMethod(typeof(mvcapapplication2.App_Start.NinjectWebCommon),“Start”)]
[程序集:WebActivator.ApplicationShutdownMethodAttribute(typeof(mvcapApplication2.App_Start.NinjectWebCommon),“停止”)]
名称空间MVCAPApplication2.App\u开始
{
使用制度;
使用System.Web;
使用Microsoft.Web.Infrastructure.DynamicModuleHelper;
使用Ninject;
使用Ninject.Web.Common;
公共静态类NinjectWebCommon
{
私有静态只读引导程序Bootstrapper=new Bootstrapper();
///
///启动应用程序
///
公共静态void Start()
{
RegisterModule(typeof(OnePerRequestHttpModule));
RegisterModule(typeof(NinjectHttpModule));
初始化(CreateKernel);
}
///
///停止应用程序。
///
公共静态无效停止()
{
bootstrapper.ShutDown();
}
///
///创建将管理应用程序的内核。
///
///创建的内核。
私有静态IKernel CreateKernel()
{
var kernel=新的标准内核();
kernel.Bind().ToMethod(ctx=>()=>newbootstrapper().kernel);
kernel.Bind().To();
//注册依赖项
注册服务(内核);
//设置Web API解析器
GlobalConfiguration.Configuration.DependencyResolver=新的NinjectDependencyResolver(内核);
返回内核;
}
///
///在这里加载您的模块或注册您的服务!
///
///内核。
私有静态无效注册服务(IKernel内核)
{
kernel.Bind().To().InSingletonScope();
}
}
}
ILogger和Logger对象不做任何事情,但说明了问题。Logger会调试.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)
{
}
}
公共类值控制器:ApiController
{
专用只读ILogger\u记录器;
公共价值控制器(ILogger记录器)
{
_记录器=记录器;
_logger.Log(“在“+System.DateTime.Now.ToLongTimeString()创建的记录器”);
}
//获取api/值
公共IEnumerable Get()
{
返回新字符串[]{“value1”,“value2”};
}
//获取api/values/5
公共字符串Get(int-id)
{
返回“值”;
}
//后api/值
公共作废帖子(字符串值)
{
}
//将api/values/5放入
公共void Put(int-id,字符串值)
{
}
//删除api/values/5
公共无效删除(int-id)
{
}
}
当我将跟踪信息放入内核的创建中时,它似乎表明内核只创建了一次。所以我没有看到什么?为什么单例不能持久化?使用
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(kernel);
}
并且不要在NinjectDependencyScope
@Remo-Gloor中处理内核
当我在WebAPI的MemoryHost中运行您的代码并运行集成测试时,一切都很好,我有singleton。
如果我在VS Cassini web服务器内部运行WebAPI解决方案,则第一次运行成功
当我单击“刷新”时,我收到异常:
加载Ninject组件ICache时出错
内核的组件容器中没有注册此类组件。
如果我返回带有BeginBlock的旧代码,它在Cassini中有效,但在集成测试中不再使用singleton。您可以简单地实现自己的singleton,而不是不处理内核(不会调用内部dispose)
public static class NinjectSingletonExtension
{
public static CustomSingletonKernelModel<T> SingletonBind<T>(this IKernel i_KernelInstance)
{
return new CustomSingletonKernelModel<T>(i_KernelInstance);
}
}
public class CustomSingletonKernelModel<T>
{
private const string k_ConstantInjectionName = "Implementation";
private readonly IKernel _kernel;
private T _concreteInstance;
public CustomSingletonKernelModel(IKernel i_KernelInstance)
{
this._kernel = i_KernelInstance;
}
public IBindingInNamedWithOrOnSyntax<T> To<TImplement>(TImplement i_Constant = null) where TImplement : class, T
{
_kernel.Bind<T>().To<TImplement>().Named(k_ConstantInjectionName);
var toReturn =
_kernel.Bind<T>().ToMethod(x =>
{
if (i_Constant != null)
{
return i_Constant;
}
if (_concreteInstance == null)
{
_concreteInstance = _kernel.Get<T>(k_ConstantInjectionName);
}
return _concreteInstance;
}).When(x => true);
return toReturn;
}
}
公共静态类NinjectSingletonExtension
{
公共静态CustomSingletonKernelModelSingletonBind(此IKernel i_KernelInstance)
{
返回新的CustomSingletonKernelModel(i_KernelInstance);
}
}
公共类CustomSingletonKernelModel
{
私有常量字符串k_ConstantInjectionName=“实现”;
私有只读IKernel_内核;
私有T_实例;
公共定制单一内核模型(IKernel i_KernelInstance)
{
这个。_kernel=i_KernelInstance;
}
公共IBindingInNamedWithOrOnSyntax To(TImplement i_常量=null),其中TImplement:class,T
{
_kernel.Bind().To().Named(k_ConstantInjectionName);
var toReturn=
_kernel.Bind().ToMethod(x=>
{
if(i_常量!=null)
{
返回i_常数;
}
如果(_concreteInstance==null)
{
_concreteInstance=_kernel.Get(k_ConstantInjectionName);
}
返回具体实例;
}).When(x=>true);
返回
public static class NinjectSingletonExtension
{
public static CustomSingletonKernelModel<T> SingletonBind<T>(this IKernel i_KernelInstance)
{
return new CustomSingletonKernelModel<T>(i_KernelInstance);
}
}
public class CustomSingletonKernelModel<T>
{
private const string k_ConstantInjectionName = "Implementation";
private readonly IKernel _kernel;
private T _concreteInstance;
public CustomSingletonKernelModel(IKernel i_KernelInstance)
{
this._kernel = i_KernelInstance;
}
public IBindingInNamedWithOrOnSyntax<T> To<TImplement>(TImplement i_Constant = null) where TImplement : class, T
{
_kernel.Bind<T>().To<TImplement>().Named(k_ConstantInjectionName);
var toReturn =
_kernel.Bind<T>().ToMethod(x =>
{
if (i_Constant != null)
{
return i_Constant;
}
if (_concreteInstance == null)
{
_concreteInstance = _kernel.Get<T>(k_ConstantInjectionName);
}
return _concreteInstance;
}).When(x => true);
return toReturn;
}
}
i_Kernel.SingletonBind<T>().To<TImplement>();
i_Kernel.Bind<T>().To<TImplement>().InSingletonScope();