Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I assume IProvidePrincipal is your own implementation. Best way, the only one IMHO, to use an IoC container is the <a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot/" rel="noreferrer">Composition Root</a>. The entry point/composition root for web api has been well explained by <a href="http://blog.ploeh.dk/2012/10/03/DependencyInjectioninASP.NETWebAPIwithCastleWindsor/" rel="noreferrer">ploeh's blog</a>. DelegatingHandler are not part of the "request resolving", so you may choose to resolve it within global asax Application_start where the container lives as private variable.</p> <pre><code>GlobalConfiguration.Configuration.MessageHandlers.Add(container.Resolve&lt;BasicAuthMessageHandler&gt;()); </code></pre> <p>If you properly registered your handler and all its dependencies in the container, nothing else has to be done: handler instance you extracted from the container and added among MessageHandlers will have an IPrincipalProvider and (I)UserRepository. Keep in mind BasicAuthMessageHandler will act a singleton, so if you need a new instance of (I)UserRepository on each method call... you may consider <a href="http://docs.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx?HL=typedfactory" rel="noreferrer">TypedFactory</a> to create your (I)UserRepository as late dependencies</p> <p>Of course, any component starting from you top graph component have to be <a href="http://docs.castleproject.org/Windsor.Three-Calls-Pattern.ashx" rel="noreferrer">register in the container</a>.</p> <p>That's the easy way... in case you need somenthing more sophisticate, you may end up creating your "composition root" for DelegatingHandlers as well.</p> <p>BTW: never, ever, doing somenthing like UserRepository userRepo = new UserRepository(); or PrincipalProvider = new DummyPrincipalProvider()</p> <p>none of the "Behaviour instance" should be created explicitly: container take care of providing right instance at the right time...</p> <p><strong>As per Jon Edit:</strong> now DummyPrincipalProvider looks fine: just keep in mind since DummyPrincipalProvider is created among the message handler(act as singleton due to global registration), you are reusing always same instance.</p> <p>Instead of your plumbing</p> <pre><code>var dependencyResolver = new WindsorDependencyResolver(_container); configuration.DependencyResolver = dependencyResolver; </code></pre> <p>I rather use ploeh implementation(see above).</p> <p>Your registration</p> <pre><code>container.Register( Component.For&lt;IProvidePrincipal&gt;().ImplementedBy&lt;DummyPrincipalProvider&gt;() .UsingFactoryMethod(kernel =&gt; kernel.Resolve&lt;DummyPrincipalProvider&gt;()) ); </code></pre> <p>should be replaced with</p> <pre><code>container.Register( Component.For&lt;IProvidePrincipal&gt;().ImplementedBy&lt;DummyPrincipalProvider&gt;() ); </code></pre> <p>that's wrong... container has to resolve it, not you explicitly</p> <pre><code>GlobalConfiguration.Configuration.MessageHandlers.Add(new BasicAuthMessageHandler()); </code></pre> <p>stick with my configuration as above: BasicAuthMessageHandler resolved via container.</p> <p>Let me know if it works.</p> <p>PS: You registered the TypedFactory facility in the container, but you are not using it... just to let you know. You registered DelegatingHandler(s) as Transient, but keep in mind they gonna be "singleton" by design: adding it to the MessageHandlers collection imply they gonna be reused on each request.</p> <p><strong>As per Jon Edit 2:</strong></p> <p>I added a <a href="https://github.com/cristiano-degiorgis/WebApi4-PrincipalProvider" rel="noreferrer">sample on github</a>. You should be able to build it and run it using NuGet Package Restore</p> <p>Your issue about PerWebRequestdepends on the depencies of UserRepository on the NHibernate factory session creating session "PerWebRequest": you cannot resolve IPrincipalProvider->IUserRepository->ISession in Application_Start due to HttpContext. If you really need a IUserRepositry working w/ IPrincipalProvider dependency has to be to a IUserRepositoryFactory(TypedFactory) instead. I tried to fix your sample using the typed factory and it works, but than I had an issue w/ NHibernate configuration and since I'm not an expert of that, I didn't go any further. </p> <p>If you need help w/ the factory thing... LMK and I'll update my git sample using a factory within the DummyPrincipalProvider</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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