c# - 在为 ASP.NET MVC 2.0 应用程序注册区域时提供或过滤程序集

标签 c# asp.net-mvc-2 registration asp.net-mvc-areas

我有一个大型应用程序,目前是 WebForms 和 MVC 2.0 的混合体。我的应用程序的启动很糟糕,罪魁祸首主要是因为 AreaRegistration.RegisterAllAreas 调用。更具体地说,它正在使用 System.Web。 Compilation.BuildManager.GetReferencedAssemblies 枚举应用程序直接引用的程序集中的所有类型,并测试它们是否派生自 AreaRegistration

不幸的是,我有许多第三方程序集恰好非常广泛,所以这个初始负载可能非常糟糕。如果我可以告诉它要查找哪些程序集 AreaRegistrations,或者暂时手动注册区域,我会得到更好的结果。

我可以收集 AreaRegistration 的所有内部信息来创建和调用注册,但我很好奇是否其他人已经遇到并解决了这个问题。

最佳答案

我整理了以下实用程序来隔离用于注册区域的程序集。我不得不破解区域注册的内部结构,但它们似乎并不复杂,而且对我来说运行得相当好:

using System;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;

namespace MyCompany.Web.Mvc
{
    /// <summary>
    /// Provides helpful utilities for performing area registration, where <see cref="AreaRegistration.RegisterAllAreas()"/> may not suffice.
    /// </summary>
    public static class AreaRegistrationUtil
    {
        /// <summary>
        /// Registers all areas found in the assembly containing the given type.
        /// </summary>
        /// <typeparam name="T">A type that derives from <see cref="AreaRegistration"/> and has a default constructor.</typeparam>
        public static void RegisterAreasForAssemblyOf<T>()
            where T : AreaRegistration, new()
        {
            RegisterAreasForAssemblyOf<T>(null);
        }

        /// <summary>
        /// Registers all areas found in the assembly containing the given type.
        /// </summary>
        /// <typeparam name="T">A type that derives from <see cref="AreaRegistration"/> and has a default constructor.</typeparam>
        /// <param name="state">An object containing state that will be passed to the area registration.</param>
        public static void RegisterAreasForAssemblyOf<T>(object state)
            where T : AreaRegistration, new()
        {
            RegisterAreasForAssemblies(state, typeof (T).Assembly);
        }

        /// <summary>
        /// Registers all areas found in the given assemblies.
        /// </summary>
        /// <param name="assemblies"><see cref="Assembly"/> objects containing the prospective area registrations.</param>
        public static void RegisterAreasForAssemblies(params Assembly[] assemblies)
        {
            RegisterAreasForAssemblies(null, assemblies);
        }

        /// <summary>
        /// Registers all areas found in the given assemblies.
        /// </summary>
        /// <param name="state">An object containing state that will be passed to the area registration.</param>
        /// <param name="assemblies"><see cref="Assembly"/> objects containing the prospective area registrations.</param>
        public static void RegisterAreasForAssemblies(object state, params Assembly[] assemblies)
        {
            foreach (Type type in
                from assembly in assemblies
                from type in assembly.GetTypes()
                where IsAreaRegistrationType(type)
                select type)
            {
                RegisterArea((AreaRegistration) Activator.CreateInstance(type), state);
            }
        }

        /// <summary>
        /// Performs area registration using the specified type.
        /// </summary>
        /// <typeparam name="T">A type that derives from <see cref="AreaRegistration"/> and has a default constructor.</typeparam>
        public static void RegisterArea<T>()
            where T : AreaRegistration, new()
        {
            RegisterArea<T>(null);
        }

        /// <summary>
        /// Performs area registration using the specified type.
        /// </summary>
        /// <typeparam name="T">A type that derives from <see cref="AreaRegistration"/> and has a default constructor.</typeparam>
        /// <param name="state">An object containing state that will be passed to the area registration.</param>
        public static void RegisterArea<T>(object state)
            where T : AreaRegistration, new()
        {
            var registration = Activator.CreateInstance<T>();
            RegisterArea(registration, state);
        }

        private static void RegisterArea(AreaRegistration registration, object state)
        {
            var context = new AreaRegistrationContext(registration.AreaName, RouteTable.Routes, state);
            string ns = registration.GetType().Namespace;

            if (ns != null) context.Namespaces.Add(string.Format("{0}.*", ns));

            registration.RegisterArea(context);
        }

        /// <summary>
        /// Returns whether or not the specified type is assignable to <see cref="AreaRegistration"/>.
        /// </summary>
        /// <param name="type">A <see cref="Type"/>.</param>
        /// <returns>True if the specified type is assignable to <see cref="AreaRegistration"/>; otherwise, false.</returns>
        private static bool IsAreaRegistrationType(Type type)
        {
            return (typeof (AreaRegistration).IsAssignableFrom(type) && (type.GetConstructor(Type.EmptyTypes) != null));
        }
    }
}

对我来说,最简单的使用方法是

AreaRegistrationUtil.RegisterAreasForAssemblyOf<SomeTypeInTargetAssembly>();

这显着缩短了启动时间,但代价是无法进入某个区域并让应用程序自动注册它。但是,在这种情况下,这不是我关心的问题。

关于c# - 在为 ASP.NET MVC 2.0 应用程序注册区域时提供或过滤程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2496956/

相关文章:

c# - 如何在 C# 中构建 Web 服务缓存

c# - 最佳优先搜索评价函数

C# 数据表 LINQ 动态 row.field<datatype> 需要编码

c# - Unity DI 中的 AutoMapper 注册

php - 第 1 行的 MySQL 语法错误和反引号提供奇怪的解决方案

c# - 读取 byte[] 最快(可能不安全)的方法是什么?

asp.net-mvc-2 - 需要帮助将 DefaultModelBinder 用于嵌套模型

asp.net - 将 View 模型从 ajax 发送到 Controller

asp.net-mvc-2 - 有人尝试过 Codeplex 的新 MVC HTML5 工具包吗?

php - 查找数组中时间范围内的记录