Skip to content

Instantly share code, notes, and snippets.

@iamkoch
Created April 9, 2012 16:41
Show Gist options
  • Save iamkoch/2344638 to your computer and use it in GitHub Desktop.
Save iamkoch/2344638 to your computer and use it in GitHub Desktop.
Register all assemblies in MVC Bin folder for Castle.Windsor DI
using System;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Installer;
using WednesdayFootball.Factory;
[assembly: WebActivator.PreApplicationStartMethod(typeof(WednesdayFootball.AppStart.DependencyInjection), "BootstrapContainer")]
namespace WednesdayFootball.AppStart
{
public static class DependencyInjection
{
private static void BootstrapContainer()
{
var container = new WindsorContainer().Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
RegisterTypes(container);
}
private static void RegisterTypes(IWindsorContainer container)
{
container
.Register(AllTypes
.FromAssemblyInDirectory(new AssemblyFilter(AssemblyDirectory))
.Pick()
.If(x => x.IsPublic)
.If(x => x.GetInterfaces().Length > 0)
.WithService
.FirstInterface()
.LifestyleTransient());
}
static public string AssemblyDirectory
{
get
{
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment