Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As discussed in <a href="https://stackoverflow.com/a/15604758/11635">the other answer</a>, <code>InCallScope</code> is not a good approach to solving this problem.</p> <p>For now I'm dumping some code that works against the latest NuGet Unstable / Include PreRelease / <code>Instal-Package -Pre</code> editions of <code>Ninject.Web.Common</code> without a clear explanation. I <strike>will translate this to an article in the <code>Ninject.Extensions.NamedScope</code> wiki at some stage</strike>have started to write a walkthrough of this technique in <a href="https://github.com/ninject/ninject.extensions.namedscope/wiki/Createnamedscope-getscope" rel="nofollow noreferrer">the <code>Ninject.Extensions.NamedScope</code> wiki's CreateNamedScope/GetScope article</a>.</p> <p>Possibly some bits will become Pull Request(s) at some stage too (Hat tip to @Remo Gloor who supplied me the outline code). The <a href="https://gist.github.com/bartelink/5671464" rel="nofollow noreferrer">associated tests and learning tests are in this gist for now</a>), pending packaging in a proper released format TBD. </p> <p>The exec summary is you Load the Module below into your Kernel and use <code>.InRequestScope()</code> on everything you want created / <code>Dispose</code>d per handler invocation and then feed requests through via <code>IHandlerComposer.ComposeCallDispose</code>.</p> <p>If you use the following Module:</p> <pre><code>public class Module : NinjectModule { public override void Load() { Bind&lt;IHandlerComposer&gt;().To&lt;NinjectRequestScopedHandlerComposer&gt;(); // Wire it up so InRequestScope will work for Handler scopes Bind&lt;INinjectRequestHandlerScopeFactory&gt;().To&lt;NinjectRequestHandlerScopeFactory&gt;(); NinjectRequestHandlerScopeFactory.NinjectHttpApplicationPlugin.RegisterIn( Kernel ); } } </code></pre> <p>Which wires in a Factory[1] and <code>NinjectHttpApplicationPlugin</code> that exposes:</p> <pre><code>public interface INinjectRequestHandlerScopeFactory { NamedScope CreateRequestHandlerScope(); } </code></pre> <p>Then you can use this Composer to Run a Request <code>InRequestScope()</code>:</p> <pre><code>public interface IHandlerComposer { void ComposeCallDispose( Type type, Action&lt;object&gt; callback ); } </code></pre> <p>Implemented as:</p> <pre><code>class NinjectRequestScopedHandlerComposer : IHandlerComposer { readonly INinjectRequestHandlerScopeFactory _requestHandlerScopeFactory; public NinjectRequestScopedHandlerComposer( INinjectRequestHandlerScopeFactory requestHandlerScopeFactory ) { _requestHandlerScopeFactory = requestHandlerScopeFactory; } void IHandlerComposer.ComposeCallDispose( Type handlerType, Action&lt;object&gt; callback ) { using ( var resolutionRoot = _requestHandlerScopeFactory.CreateRequestHandlerScope() ) foreach ( object handler in resolutionRoot.GetAll( handlerType ) ) callback( handler ); } } </code></pre> <p>The Ninject Infrastructure stuff:</p> <pre><code>class NinjectRequestHandlerScopeFactory : INinjectRequestHandlerScopeFactory { internal const string ScopeName = "Handler"; readonly IKernel _kernel; public NinjectRequestHandlerScopeFactory( IKernel kernel ) { _kernel = kernel; } NamedScope INinjectRequestHandlerScopeFactory.CreateRequestHandlerScope() { return _kernel.CreateNamedScope( ScopeName ); } /// &lt;summary&gt; /// When plugged in as a Ninject Kernel Component via &lt;c&gt;RegisterIn(IKernel)&lt;/c&gt;, makes the Named Scope generated during IHandlerFactory.RunAndDispose available for use via the Ninject.Web.Common's &lt;c&gt;.InRequestScope()&lt;/c&gt; Binding extension. /// &lt;/summary&gt; public class NinjectHttpApplicationPlugin : NinjectComponent, INinjectHttpApplicationPlugin { readonly IKernel kernel; public static void RegisterIn( IKernel kernel ) { kernel.Components.Add&lt;INinjectHttpApplicationPlugin, NinjectHttpApplicationPlugin&gt;(); } 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() { } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload