How do I use Windsor as IoC?http://forums.asp.net/t/1772519.aspx/1?How+do+I+use+Windsor+as+IoC+Thu, 23 Feb 2012 06:34:13 -050017725194845155http://forums.asp.net/p/1772519/4845155.aspx/1?How+do+I+use+Windsor+as+IoC+How do I use Windsor as IoC? <p>Hi</p> <p>How do I use Castle:Windsor as IoC.. I've tried doing the following:</p> <pre class="prettyprint">public static void Start() { Container.Register(Component.For&lt;IHttpControllerFactory&gt;().ImplementedBy&lt;CustomControllerFactory&gt;().LifeStyle.Singleton); Container.Register(Component.For&lt;ILogger&gt;().ImplementedBy&lt;Log4NetLogger&gt;().LifeStyle.Singleton); Container.Register(Component.For&lt;IFormatterSelector&gt;().ImplementedBy&lt;FormatSelector&gt;().LifeStyle.Singleton); Container.Register(Component.For&lt;IHttpController&gt;().ImplementedBy&lt;BooksController&gt;().Named(&quot;Books&quot;).LifeStyle.Transient); var config = GlobalConfiguration.Configuration; config.ServiceResolver.SetResolver(t =&gt; Container.Resolve(t), t =&gt; Container.ResolveAll(t).Cast&lt;object&gt;()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); BundleTable.Bundles.RegisterTemplateBundles(); }</pre> <pre class="prettyprint"> public class CustomControllerFactory : IHttpControllerFactory { private readonly IKernel kernel; public CustomControllerFactory(IKernel kernel) { this.kernel = kernel; } public IHttpController CreateController(HttpControllerContext controllerContext, string controllerName) { var res= kernel.Resolve&lt;IHttpController&gt;(controllerName); return res; } public void ReleaseController(IHttpController controller) { kernel.ReleaseComponent(controller); } } </pre> <p></p> <p>but I'm getting a 'Object reference not set to an instance of an object.' error all the time... eventhough I can see that the container i resolving the type...</p> <p>Also it seems like I have to register a ILogger and IFormatSelector into the container... but do I have to implement these myself or do can I just some default ones?</p> <p></p> <p>TIA</p> <p></p> <p>Søren</p> <p></p> 2012-02-22T11:58:03-05:004845374http://forums.asp.net/p/1772519/4845374.aspx/1?Re+How+do+I+use+Windsor+as+IoC+Re: How do I use Windsor as IoC? <p>Ahh.... seems like I have to register a lot of different stuff to make it work:</p> <p></p> <pre class="prettyprint">public static void Start() { var configuration = GlobalConfiguration.Configuration; Container.Register(Component.For&lt;IHttpControllerFactory&gt;().ImplementedBy&lt;CustomControllerFactory&gt;().LifeStyle.Singleton); Container.Register(Component.For&lt;ILogger&gt;().ImplementedBy&lt;Log4NetLogger&gt;().LifeStyle.Singleton); Container.Register(Component.For&lt;IFormatterSelector&gt;().ImplementedBy&lt;FormatterSelector&gt;().LifeStyle.Singleton); Container.Register(Component.For&lt;IHttpControllerActivator&gt;().ImplementedBy&lt;DefaultHttpControllerActivator&gt;().LifeStyle.Transient); Container.Register(Component.For&lt;IHttpActionSelector&gt;().ImplementedBy&lt;ApiControllerActionSelector&gt;().LifeStyle.Transient); Container.Register(Component.For&lt;IActionValueBinder&gt;().ImplementedBy&lt;DefaultActionValueBinder&gt;().LifeStyle.Transient); Container.Register(Component.For&lt;IHttpActionInvoker&gt;().ImplementedBy&lt;ApiControllerActionInvoker&gt;().LifeStyle.Transient); Container.Register(Component.For&lt;System.Web.Http.Metadata.ModelMetadataProvider&gt;().ImplementedBy&lt;System.Web.Http.Metadata.Providers.CachedDataAnnotationsModelMetadataProvider&gt;().LifeStyle.Transient); Container.Register(Component.For&lt;HttpConfiguration&gt;().Instance(configuration)); Container.Register(Component.For&lt;IHttpController&gt;().ImplementedBy&lt;BooksService&gt;().Named(&quot;Books&quot;).LifeStyle.Transient); configuration.ServiceResolver.SetResolver(t =&gt; Container.Resolve(t), t =&gt; Container.ResolveAll(t).Cast&lt;object&gt;()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); BundleTable.Bundles.RegisterTemplateBundles(); }</pre> <pre class="prettyprint">and follow what 'j<a title="james-world" href="http://forums.asp.net/members/james-world.aspx">ames-world</a>' is doing here: <a href="http://forums.asp.net/t/1770736.aspx/1?Implementing+IHttpControllerFactory+CreateController">http://forums.asp.net/t/1770736.aspx/1?Implementing+IHttpControllerFactory+CreateController</a></pre> <pre class="prettyprint"> public class CustomControllerFactory : IHttpControllerFactory { private readonly HttpConfiguration configuration; private readonly IKernel kernel; public CustomControllerFactory(HttpConfiguration configuration, IKernel kernel) { this.configuration = configuration; this.kernel = kernel; } public IHttpController CreateController(HttpControllerContext controllerContext, string controllerName) { var controller = kernel.Resolve&lt;IHttpController&gt;(controllerName); controllerContext.Controller = controller; controllerContext.ControllerDescriptor =new HttpControllerDescriptor(configuration, controllerName, controller.GetType()); return controllerContext.Controller; } public void ReleaseController(IHttpController controller) { kernel.ReleaseComponent(controller); } }</pre> <pre class="prettyprint">seems a little complicated though....</pre> 2012-02-22T13:54:21-05:004845380http://forums.asp.net/p/1772519/4845380.aspx/1?Re+How+do+I+use+Windsor+as+IoC+Re: How do I use Windsor as IoC? <p>You should return null from your resolving logic if you cannot resolve the type in question - i.e. if the type is not resolvable by the container you configured (with only the types you need) then return null. In this case the default resolver kicks in (and you do not need to care about the rest...).</p> <p></p> <p>Please see samples here:&nbsp;<a href="https://github.com/ChristianWeyer/Thinktecture.Web.Http">https://github.com/ChristianWeyer/Thinktecture.Web.Http</a></p> 2012-02-22T13:57:40-05:004845460http://forums.asp.net/p/1772519/4845460.aspx/1?Re+How+do+I+use+Windsor+as+IoC+Re: How do I use Windsor as IoC? <p>Ok, so I should change my SetResolver to:</p> <pre class="prettyprint">configuration.ServiceResolver.SetResolver(t =&gt; { try { return Container.Resolve(t); } catch (ComponentNotFoundException ex) { return null; } }, t =&gt; { try { return Container.ResolveAll(t).Cast&lt;object&gt;(); } catch (ComponentNotFoundException ex) { return null; } });</pre> <p></p> <p>... yes that seems to work</p> <p>Thx,&nbsp;</p> <p>Søren</p> 2012-02-22T14:32:01-05:004845594http://forums.asp.net/p/1772519/4845594.aspx/1?Re+How+do+I+use+Windsor+as+IoC+Re: How do I use Windsor as IoC? <p>Since SetResolver sample&nbsp;does not provide an hook for &quot;Release&quot; seems to be a time-bomb with mature IoC container such Windsor... if you do not explicit call Release on root component such controller(most likely they have been created as Transient) you gonna experiencing a huge memory leaks</p> <p><span class="typ">IHttpControllerFactory</span><span class="pln"> </span>seems the only safe approach.</p> 2012-02-22T15:54:32-05:004846431http://forums.asp.net/p/1772519/4846431.aspx/1?Re+How+do+I+use+Windsor+as+IoC+Re: How do I use Windsor as IoC? <p>Ok, so the best approach would be to use:</p> <p>configuration.ServiceResolver.SetService(typeof (IHttpControllerFactory), new CustomControllerFactory(configuration, Container));</p> <p>rather than changing the ServiceResolver for everything.</p> <p></p> <p>Søren</p> <p></p> <p></p> 2012-02-23T06:14:44-05:004846477http://forums.asp.net/p/1772519/4846477.aspx/1?Re+How+do+I+use+Windsor+as+IoC+Re: How do I use Windsor as IoC? <pre>even better without the try catch:</pre> <pre></pre> <pre>config.ServiceResolver.SetResolver( t&nbsp;=&gt; { container.Kernel.HasComponent(t)&nbsp;?&nbsp;container.Resolve(t)&nbsp;:&nbsp;<span>null</span> }, t&nbsp;=&gt; { container.Kernel.HasComponent(t)&nbsp;?&nbsp;container.ResolveAll(t).Cast&lt;<span>object</span>&gt;()&nbsp;:&nbsp;<span>new</span>&nbsp;<span>List</span>&lt;<span>object</span>&gt;()});</pre> 2012-02-23T06:34:13-05:00