正如在另一个答案中所讨论的,InCallScope
这不是解决这个问题的好方法。
现在,我正在倾销一些适用于最新 NuGet Unstable / Include PreRelease / Instal-Package -Pre
版本的代码,Ninject.Web.Common
但没有明确的解释。我将把它翻译成 wiki 中的一篇文章,Ninject.Extensions.NamedScope
在某个阶段已经开始在 wiki 的 CreateNamedScope/GetScope 文章中编写此技术的Ninject.Extensions.NamedScope
演练。
可能一些位也会在某个阶段成为拉取请求(向@Remo Gloor 致敬,他为我提供了大纲代码)。相关的测试和学习测试现在都在这个要点中),等待以适当的发布格式打包(待定)。
执行摘要是您将下面的模块加载到您的内核中,并在每个处理程序调用中使用.InRequestScope()
您想要创建/ Dispose
d 的所有内容,然后通过IHandlerComposer.ComposeCallDispose
.
如果您使用以下模块:
public class Module : NinjectModule
{
public override void Load()
{
Bind<IHandlerComposer>().To<NinjectRequestScopedHandlerComposer>();
// Wire it up so InRequestScope will work for Handler scopes
Bind<INinjectRequestHandlerScopeFactory>().To<NinjectRequestHandlerScopeFactory>();
NinjectRequestHandlerScopeFactory.NinjectHttpApplicationPlugin.RegisterIn( Kernel );
}
}
工厂 [1] 中的哪些电线NinjectHttpApplicationPlugin
暴露:
public interface INinjectRequestHandlerScopeFactory
{
NamedScope CreateRequestHandlerScope();
}
然后你可以使用这个 Composer 来运行一个请求InRequestScope()
:
public interface IHandlerComposer
{
void ComposeCallDispose( Type type, Action<object> callback );
}
实现为:
class NinjectRequestScopedHandlerComposer : IHandlerComposer
{
readonly INinjectRequestHandlerScopeFactory _requestHandlerScopeFactory;
public NinjectRequestScopedHandlerComposer( INinjectRequestHandlerScopeFactory requestHandlerScopeFactory )
{
_requestHandlerScopeFactory = requestHandlerScopeFactory;
}
void IHandlerComposer.ComposeCallDispose( Type handlerType, Action<object> callback )
{
using ( var resolutionRoot = _requestHandlerScopeFactory.CreateRequestHandlerScope() )
foreach ( object handler in resolutionRoot.GetAll( handlerType ) )
callback( handler );
}
}
Ninject 基础设施的东西:
class NinjectRequestHandlerScopeFactory : INinjectRequestHandlerScopeFactory
{
internal const string ScopeName = "Handler";
readonly IKernel _kernel;
public NinjectRequestHandlerScopeFactory( IKernel kernel )
{
_kernel = kernel;
}
NamedScope INinjectRequestHandlerScopeFactory.CreateRequestHandlerScope()
{
return _kernel.CreateNamedScope( ScopeName );
}
/// <summary>
/// When plugged in as a Ninject Kernel Component via <c>RegisterIn(IKernel)</c>, makes the Named Scope generated during IHandlerFactory.RunAndDispose available for use via the Ninject.Web.Common's <c>.InRequestScope()</c> Binding extension.
/// </summary>
public class NinjectHttpApplicationPlugin : NinjectComponent, INinjectHttpApplicationPlugin
{
readonly IKernel kernel;
public static void RegisterIn( IKernel kernel )
{
kernel.Components.Add<INinjectHttpApplicationPlugin, NinjectHttpApplicationPlugin>();
}
public NinjectHttpApplicationPlugin( IKernel kernel )
{
this.kernel = kernel;
}
object INinjectHttpApplicationPlugin.GetRequestScope( IContext context )
{
// TODO PR for TrgGetScope
try
{
return NamedScopeExtensionMethods.GetScope( context, ScopeName );
}
catch ( UnknownScopeException )
{
return null;
}
}
void INinjectHttpApplicationPlugin.Start()
{
}
void INinjectHttpApplicationPlugin.Stop()
{
}
}
}