提供或过滤组件为ASP.NET MVC 2.0应用领域注册时 [英] Providing or Filtering assemblies when registering areas for an ASP.NET MVC 2.0 application

查看:110
本文介绍了提供或过滤组件为ASP.NET MVC 2.0应用领域注册时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前存在的WebForms和MVC 2.0的混合的大型应用程序。我的应用程序的启动是可怕的,而罪魁祸首主要是 AreaRegistration.RegisterAllAreas 通话,因为。更具体地讲,它是使用的System.Web。 Compilation.BuildManager.GetReferencedAssemblies 来枚举由应用程序直接引用的程序集的所有类型和对其进行测试,看看他们是否从 AreaRegistration 派生。

I have a large application that currently exists as a hybrid of WebForms and MVC 2.0. Startup of my application is dreadful, and the culprit is primarily because of the AreaRegistration.RegisterAllAreas call. More specifically, that it is using the System.Web. Compilation.BuildManager.GetReferencedAssemblies to enumerate all types in assemblies directly referenced by the application and test them to see if they derive from AreaRegistration.

不幸的是,我有一些碰巧是相当广泛的第三方组件,所以这初次负荷量可pretty坏。我有更好的结果,如果我可以告诉它组件寻找 AreaRegistrations ,甚至手动注册地区暂且。

Unfortunately, I have a number of third-party assemblies that happen to be quite extensive, so this initial load can be pretty bad. I'd have much better results if I could tell it which assemblies to look for AreaRegistrations, or even register areas manually for the time being.

我可以收集所有 AreaRegistration 的内部创建和调用登记,但我只是好奇,如果别人已经并解决此问题的工作。

I can gather up all the internals of AreaRegistration to create and invoke the registration, but I'm just curious if others have had and worked around this issue.

推荐答案

我总结了以下实用工具来隔离组件用于注册的地区。我不得不砍出区登记的内部,但他们似乎并不十分复杂,这一直是我的合理运行良好:

I put together the following utility to isolate Assemblies for registering Areas. I had to hack out the internals of area registration, but they don't seem terribly complicated and this has been running reasonably well for me:

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));
        }
    }
}

使用这个,对我来说最简单的办法,就是

Easiest way to use this, for me, is

AreaRegistrationUtil.RegisterAreasForAssemblyOf<SomeTypeInTargetAssembly>();

这已启动时间取得明显改善,在不能够在一个地区下降并让应用程序自动注册它的代价。然而,这不是我的在这种情况下,关注的问题。

This has made noticeable improvements in start up time, at the expense of not being able to drop in an area and have the application automatically register it. However, that isn't a concern of mine in this case.

这篇关于提供或过滤组件为ASP.NET MVC 2.0应用领域注册时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆