csharp 用于ASP.NET MVC的Castle Windsor IoC容器设置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 用于ASP.NET MVC的Castle Windsor IoC容器设置相关的知识,希望对你有一定的参考价值。

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()
				.Pick().If(t => t.Name.EndsWith("Controller"))
				.Configure(configurer => configurer.Named(configurer.Implementation.Name))
				.LifestylePerWebRequest());
		}
	} 
}
void Application_Start(object sender, EventArgs e)
{
	AreaRegistration.RegisterAllAreas();

	RegisterGlobalFilters(GlobalFilters.Filters);
	RegisterRoutes(RouteTable.Routes);

        // Add this line to Application_Start in Global.asax.cs to setup the IoC Container.
	IocContainer.Setup();
}
using System.Web.Mvc;
using Castle.Windsor;
using Castle.Windsor.Installer;

namespace MyApp.Web.PresentationLogic.Container
{
	public static class IocContainer
	{
		private static IWindsorContainer _container;

		public static void Setup()
		{
			_container = new WindsorContainer().Install(FromAssembly.This());

			WindsorControllerFactory controllerFactory = new WindsorControllerFactory(_container.Kernel);
			ControllerBuilder.Current.SetControllerFactory(controllerFactory);
		}
	}
}
# 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`](https://gist.github.com/3128275#file_business_logic_installer.cs)  
-------- [`ControllersInstaller.cs`](https://gist.github.com/3128275#file_controllers_installer.cs)  
-------- [`IocContainer.cs`](https://gist.github.com/3128275#file_ioc_container.cs)  
-------- [`WindsorControllerFactory.cs`](https://gist.github.com/3128275#file_windsor_controller_factory.cs)  

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

3. Call `IocContainer.Setup` from [`Global.asax.cs`](https://gist.github.com/3128275#file_global.asax.cs). Example shown in this Gist.

## That's it!
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);
		}
	}
}

以上是关于csharp 用于ASP.NET MVC的Castle Windsor IoC容器设置的主要内容,如果未能解决你的问题,请参考以下文章

csharp ASP.NET MVC ActionFilter允许或拒绝具有可选子网掩码的IPv4地址。用于AppHarbor或任何其他服务器解决方案

csharp ASP.NET的助手,ASP.NET MVC应用服务器端测试

csharp ASP.NET MVC 5表单

csharp asp.net mvc模块属性

csharp ASP.NET MVC WebViewPageExtensions

csharp 典型的ASP.NET MVC捆绑类