Tim*_*Tim 13 .net c# ninject asp.net-mvc-3 asp.net-web-api
我正在使用Self Hosting方法创建一个具有ASP.NET Web API接口的应用程序.我想使用类似于InRequestScope()
MVC3提供的范围.当我在IIS上托管Web API应用程序时,Ninject.Extension.WebAPI似乎支持它.但是当我自己托管WebAPI时,我在创建绑定时会得到一个新实例InRequestScope()
.当我自己托管Web API时,有什么办法可以使用这个范围吗?
Rem*_*oor 11
您可以使用NamedScope扩展来定义控制器定义范围,并将该范围用于请求范围中的所有内容.最好使用此定义的约定:
const string ControllerScope = "ControllerScope";
kernel.Bind(x => x.FromThisAssembly()
.SelectAllClasses().InheritedFrom<ApiController>()
.BindToSelf()
.Configure(b => b.DefinesNamedScope(ControllerScope)));
kernel.Bind<IMyComponent>().To<MyComponent>().InNamedScope(ControllerScope);
Run Code Online (Sandbox Code Playgroud)
我建议INotifyWhenDisposed
为控制器实现,以便请求范围内的对象在请求后立即释放.例如,通过从以下类派生而不是ApiController
public abstract class NinjectApiController : ApiController, INotifyWhenDisposed
{
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
this.IsDisposed = true;
this.Disposed(this, EventArgs.Empty);
}
public bool IsDisposed
{
get;
private set;
}
public event EventHandler Disposed;
}
Run Code Online (Sandbox Code Playgroud)
我试图在即将到来的几周内为WebAPI自主托管提供扩展.
编辑:
自我托管支持现在由Ninject.Web.WebApi.Selfhosting提供 https://nuget.org/packages/Ninject.Web.WebApi.Selfhost/3.0.2-unstable-0
示例:https://github.com/ninject/Ninject.Web.WebApi/tree/master/src/Ninject.Web.WebApi.Selfhost
归档时间: |
|
查看次数: |
1549 次 |
最近记录: |