Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adautoneto/3298795 to your computer and use it in GitHub Desktop.
Save adautoneto/3298795 to your computer and use it in GitHub Desktop.
Castle Windsor IoC Container setup for ASP.NET MVC

Castle Windsor IoC Container setup for ASP.NET MVC

  1. Create the following folder structure and files:

-- MyApp.Web (The MVC project)
---- PresentationLogic (folder)
------ Container (folder)
-------- BusinessLogicInstaller.cs
-------- ControllersInstaller.cs
-------- IocContainer.cs
-------- WindsorControllerFactory.cs

  1. Add the contents of each file in this Gist to your app. Rename namespace to fit to your application.

  2. Call IocContainer.Setup from Global.asax.cs. Example shown in this Gist.

That's it!

using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using MyApp.BusinessLogic.Facades;
namespace MyApp.Web.PresentationLogic.Container
{
public class BusinessLogicInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<IProductBusinessFacade>().ImplementedBy<ProductBusinessFacade>());
}
}
}
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
namespace MyApp.Web.PresentationLogic.Container
{
public class ControllersInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly()
.BasedOn<IController>()
.If(t => t.Name.EndsWith("Controller"))
.Configure(_ => _.LifeStyle.PerWebRequest));
}
}
}
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
IocContainer.Setup();
}
protected void Application_End()
{
IocContainer.Dispose();
}
}
using System.Web.Mvc;
using Castle.Windsor;
using Castle.Windsor.Installer;
namespace Venus.Web.Plumbing
{
public static class IocContainer
{
private static IWindsorContainer _container;
public static void Setup()
{
_container = new WindsorContainer().Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(_container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
public static void Dispose()
{
_container.Dispose();
}
}
}
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.MicroKernel;
namespace MyApp.Web.PresentationLogic.Container
{
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel;
public WindsorControllerFactory(IKernel kernel)
{
this._kernel = kernel;
}
public override void ReleaseController(IController controller)
{
_kernel.ReleaseComponent(controller);
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
}
return (IController)_kernel.Resolve(controllerType);
}
}
}
@NemoFlying
Copy link

How netCore work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment