问题
I was taking a look at ASP.NET MVC 4 RC and cannot find DefaultHttpControllerFactory or even IHttpControllerFactory to allow my IoC container of choice (Castle Windsor) to hook into the framework for Web Api controllers. I ended up using IDependencyResolver which makes releasing components a bit trickier, but ended up with the following. Is it going to work / not memory leak until IDependencyResolver has a Release method? Global.asax ends up as:
public class WebApiApplication : System.Web.HttpApplication
{
private IWindsorContainer container;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Windsor();
}
protected void Application_End()
{
container.Dispose();
}
private void Windsor()
{
container = new WindsorContainer().Install(FromAssembly.This());
// mvc:
var mvcControllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(mvcControllerFactory);
// web api:
var httpDependencyResolver = new WindsorHttpDependencyResolver(container.Kernel);
GlobalConfiguration.Configuration.DependencyResolver = httpDependencyResolver;
}
}
WindsorControllerFactory is extension of DefaultControllerFactory for Mvc Controllers and there is a Windsor installer for them. WindsorHttpDependencyResolver ends up as:
public class WindsorHttpDependencyResolver : System.Web.Http.Dependencies.IDependencyResolver
{
private readonly IKernel kernel;
public WindsorHttpDependencyResolver(IKernel kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return kernel.Resolve<IDependencyScope>(); // instances released suitably (at end of web request)
}
public object GetService(Type serviceType)
{
// for ModelMetadataProvider and other MVC related types that may have been added to the container
// check the lifecycle of these registrations
return kernel.HasComponent(serviceType) ? kernel.Resolve(serviceType) : null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.HasComponent(serviceType) ? kernel.ResolveAll(serviceType) as IEnumerable<object> : Enumerable.Empty<object>();
}
public void Dispose()
{
// Nothing created so nothing to dispose - kernel will take care of its own
}
}
Theoretically, this means Windsor will now provide the Api Controllers, once they are installed:
public class ApiControllersInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ITypedFactoryComponentSelector>().ImplementedBy<WebApiTypedFactoryComponentSelector>());
container.Register(Component.For<IDependencyScope>().AsFactory(tfc => tfc.SelectedWith<WebApiTypedFactoryComponentSelector>()).LifestylePerWebRequest());
container.Register(Classes.FromAssemblyContaining<ValuesController>().BasedOn<IHttpController>().LifestyleTransient());
}
}
I'm using the typed factory facility to implement IDependencyScope for me, which means when the framework disposes it at the end of a request, it will release the controller and its dependencies implicitly. By using a Per Web Request life cycle, Windsor will release the factory itself too. That just leaves the custom typed factory component selector as GetService(s) will not find anything in the container:
public class WebApiTypedFactoryComponentSelector : DefaultTypedFactoryComponentSelector
{
protected override string GetComponentName(System.Reflection.MethodInfo method, object[] arguments)
{
if (method.Name == "GetService" || method.Name == "GetServices")
{
return (arguments[0] as Type).FullName;
}
return base.GetComponentName(method, arguments);
}
protected override Type GetComponentType(System.Reflection.MethodInfo method, object[] arguments)
{
if (method.Name == "GetService" || method.Name == "GetServices")
{
return arguments[0] as Type;
}
return base.GetComponentType(method, arguments);
}
}
Hope this is useful.
回答1:
I ended up using code from this blog post (further enhanced by this other one by Mark Seemann), which creates a scope per request and takes care of releasing the created objects. This is a slightly different approach to the one you've taken.
来源:https://stackoverflow.com/questions/11639169/asp-net-mvc-4-rc-with-castle-windsor