我正在try 将Windsor的依赖注入连接到标准asp.net网络表单.我想我已经使用了一个HttpModule和一个CustomAttribute(代码如下所示)实现了这一点,尽管这个解决方案似乎有点笨重,我想知道是否有一个更好的支持Windsor的现成解决方案?

这里显示了几个文件

    // index.aspx.cs
    public partial class IndexPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Logger.Write("page loading");
        }

        [Inject]
        public ILogger Logger { get; set; }
    }

    // WindsorHttpModule.cs
    public class WindsorHttpModule : IHttpModule
    {
        private HttpApplication _application;
        private IoCProvider _iocProvider;

        public void Init(HttpApplication context)
        {
            _application = context;
            _iocProvider = context as IoCProvider;

            if(_iocProvider == null)
            {
                throw new InvalidOperationException("Application must implement IoCProvider");
            }

            _application.PreRequestHandlerExecute += InitiateWindsor;
        }

        private void InitiateWindsor(object sender, System.EventArgs e)
        {
            Page currentPage = _application.Context.CurrentHandler as Page;
            if(currentPage != null)
            {
                InjectPropertiesOn(currentPage);
                currentPage.InitComplete += delegate { InjectUserControls(currentPage); };
            }
        }

        private void InjectUserControls(Control parent)
        {
            if(parent.Controls != null)
            {
                foreach (Control control in parent.Controls)
                {
                    if(control is UserControl)
                    {
                        InjectPropertiesOn(control);
                    }
                    InjectUserControls(control);
                }
            }
        }

        private void InjectPropertiesOn(object currentPage)
        {
            PropertyInfo[] properties = currentPage.GetType().GetProperties();
            foreach(PropertyInfo property in properties)
            {
                object[] attributes = property.GetCustomAttributes(typeof (InjectAttribute), false);
                if(attributes != null && attributes.Length > 0)
                {
                    object valueToInject = _iocProvider.Container.Resolve(property.PropertyType);
                    property.SetValue(currentPage, valueToInject, null);
                }
            }
        }
    }

    // Global.asax.cs
    public class Global : System.Web.HttpApplication, IoCProvider
    {
        private IWindsorContainer _container;

        public override void Init()
        {
            base.Init();

            InitializeIoC();
        }

        private void InitializeIoC()
        {
            _container = new WindsorContainer();
            _container.AddComponent<ILogger, Logger>();
        }

        public IWindsorContainer Container
        {
            get { return _container; }
        }
    }

    public interface IoCProvider
    {
        IWindsorContainer Container { get; }
    }

推荐答案

我认为你基本上是在正确的轨道上——如果你还没有,我建议你看看Rhino Igloo,一个WebForms MVC框架,Here's a good blog post on this,源代码是here——Ayende(Rhino Igloo的作者)在这个项目/库中很好地解决了将Windsor与WebForms结合使用的问题.

如果要注入整个嵌套控件集,我会缓存反射信息,我怀疑这可能会有点影响性能.

最后一个春天.net以一种更面向配置的方式来实现这一点,但可能值得一看它们的实现——这里有一个很好的reference blog post个例子.

Asp.net相关问答推荐

更新剃须刀页面中图表的值

如何使用 Get-Process powershell 命令通过端口号获取进程的 ID

在具有多个项目(API/服务/数据等)的解决方案中编写 SignalR 服务的最佳方法是什么?

从 Request.Url.AbsolutePath 获取应用相对 url

为 Web 请求实现速率限制算法的最佳方法是什么?

jquery 1.9.0 和 modernizr 无法使用 ASP.NET Web 优化框架进行缩小

asp.net dropdownlist - 在 db 值之前添加空行

如果站点在 localhost 或 127.0.0.1 上运行,如何判断 ASP.NET MVC 视图

为什么默认情况下不允许 GET 请求返回 JSON?

使用 JavaScript 更改 ASP.NET 标签的可见性

如何从 URI 中删除 PROTOCOL

如何将 ASP.NET MVC5 身份验证添加到现有数据库

如何使用 asp.net 获取 html Select 的选定值

如何判断用户代理是 ipad 还是 iphone?

HttpContext.Current 在 MVC 4 项目中未解决

解析器错误:此处不允许使用_Default,因为它没有扩展类System.Web.UI.Page和 MasterType 声明

盒式磁带包与 MVC4 包

可以在不 destruct 站点的情况下将 MIME 类型添加到 web.config 吗?

以编程方式滚动到锚标记

在 Visual Studio 2012 中调试 .NET Framework 源代码?