J'ai assemblé l'utilitaire suivant pour isoler les assemblages pour l'enregistrement de zones. Je devais pirater les entrailles de l'enregistrement de la zone, mais ils ne semblent pas très compliqué et cela a fonctionné assez bien pour moi:
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));
}
}
}
manière la plus simple à utiliser, pour moi, est
AreaRegistrationUtil.RegisterAreasForAssemblyOf<SomeTypeInTargetAssembly>();
Cela a permis d'améliorer sensiblement le temps de démarrage, au prix de ne pas pouvoir tomber dans une zone et de demander à l'application de l'enregistrer automatiquement. Cependant, ce n'est pas une de mes préoccupations dans ce cas.
qu'est ce que SomeTypeInTargetAssembly? J'ai essayé d'appeler ceci à mon Global.asax mais je n'ai aucune idée de quoi il s'agit. –
C'est un type dans l'assemblage qui contient les zones. Je pense que c'est assez clair. –
Aime ça. Transformé mon init 1104ms en 14ms. –