Orchard详解--第八篇 拓展模块及引用的预处理

时间:2023-03-10 03:58:18
Orchard详解--第八篇 拓展模块及引用的预处理

  从上一篇可以看出Orchard在处理拓展模块时主要有两个组件,一个是Folder另一个是Loader,前者用于搜索后者用于加载。
  其中Folder一共有三个:Module Folder、Core Folder、ThemeFolder。Loader有引用加载器(Referenced Module Loader)、核心模块加载器(Core Module Loader)、预编译模块加载器(Precompiled Module Loader)、动态模块加载器(Dynamic Module Loader)。它们在代码里可以看到,在创建容器时对与Folder和Loader的注册:

               builder.RegisterType<ExtensionLoaderCoordinator>().As<IExtensionLoaderCoordinator>().SingleInstance();
builder.RegisterType<ExtensionMonitoringCoordinator>().As<IExtensionMonitoringCoordinator>().SingleInstance();
builder.RegisterType<ExtensionManager>().As<IExtensionManager>().SingleInstance();
{
builder.RegisterType<ExtensionHarvester>().As<IExtensionHarvester>().SingleInstance();
builder.RegisterType<ModuleFolders>().As<IExtensionFolders>().SingleInstance()
.WithParameter(new NamedParameter("paths", extensionLocations.ModuleLocations));
builder.RegisterType<CoreModuleFolders>().As<IExtensionFolders>().SingleInstance()
.WithParameter(new NamedParameter("paths", extensionLocations.CoreLocations));
builder.RegisterType<ThemeFolders>().As<IExtensionFolders>().SingleInstance()
.WithParameter(new NamedParameter("paths", extensionLocations.ThemeLocations)); builder.RegisterType<CoreExtensionLoader>().As<IExtensionLoader>().SingleInstance();
builder.RegisterType<ReferencedExtensionLoader>().As<IExtensionLoader>().SingleInstance();
builder.RegisterType<PrecompiledExtensionLoader>().As<IExtensionLoader>().SingleInstance();
builder.RegisterType<DynamicExtensionLoader>().As<IExtensionLoader>().SingleInstance();
builder.RegisterType<RawThemeExtensionLoader>().As<IExtensionLoader>().SingleInstance();
}

  这里需要注意的是,除了以上的Loader外,这里还有一个Raw Theme Extension Loader。
  接下来看Folder是如何工作的:
  三个Folder的实现可以说是一致的,都是通过ExtensionHarvester去获取一个ExtensionDescriptior列表:

  

      public IEnumerable<ExtensionDescriptor> AvailableExtensions() {
return _extensionHarvester.HarvestExtensions(_paths, DefaultExtensionTypes.Module, "Module.txt", false/*isManifestOptional*/);
}

  仅仅是路径和文件名称(Module.txt和Theme.txt)不同。所以可以这样说,模块和主题的搜索工作是通过ExtensionHarvester完成的。

     public IEnumerable<ExtensionDescriptor> HarvestExtensions(IEnumerable<string> paths, string extensionType, string manifestName, bool manifestIsOptional) {
return paths
.SelectMany(path => HarvestExtensions(path, extensionType, manifestName, manifestIsOptional))
.ToList();
}
private IEnumerable<ExtensionDescriptor> HarvestExtensions(string path, string extensionType, string manifestName, bool manifestIsOptional) {
string key = string.Format("{0}-{1}-{2}", path, manifestName, extensionType);
return _cacheManager.Get(key, true, ctx => {
if (!DisableMonitoring) {
Logger.Debug("Monitoring virtual path \"{0}\"", path);
ctx.Monitor(_webSiteFolder.WhenPathChanges(path));
}
return AvailableExtensionsInFolder(path, extensionType, manifestName, manifestIsOptional).ToReadOnlyCollection();
});
}

  从上面代码可以看出Harvester内部还使用了缓存机制,以路径、文件名称和类型(模块还是主题)来作为关键字。它缓存的内容是当前目录下的一个拓展描述列表。
  描述信息包括:

     public string Location { get; set; }
public string Id { get; set; }
public string VirtualPath { get { return Location + "/" + Id; } }
public string ExtensionType { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public string Description { get; set; }
public string Version { get; set; }
public string OrchardVersion { get; set; }
public string Author { get; set; }
public string WebSite { get; set; }
public string Tags { get; set; }
public string AntiForgery { get; set; }
public string Zones { get; set; }
public string BaseTheme { get; set; }
public string SessionState { get; set; }
public LifecycleStatus LifecycleStatus { get; set; } public IEnumerable<FeatureDescriptor> Features { get; set; }

  功能描述信息包括:

         public ExtensionDescriptor Extension { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Category { get; set; }
public int Priority { get; set; }
public IEnumerable<string> Dependencies { get; set; }

  Harvester中的GetDescriptorForExtension和GetFeaturesForExtension两个方法分别用于通过Module.txt或Theme.txt文件来获取拓展描述和功能描述信息(此处暂未对功能描述信息的依赖列表赋值)。其中拓展会作为一个默认的功能。

  Loader
  当Folder完成描述信息的构建后,Loader将通过这些描述信息来探测和加载模块。以下是Loader的接口定义:

     public interface IExtensionLoader {
int Order { get; }
string Name { get; } IEnumerable<ExtensionReferenceProbeEntry> ProbeReferences(ExtensionDescriptor extensionDescriptor);
Assembly LoadReference(DependencyReferenceDescriptor reference);
void ReferenceActivated(ExtensionLoadingContext context, ExtensionReferenceProbeEntry referenceEntry);
void ReferenceDeactivated(ExtensionLoadingContext context, ExtensionReferenceProbeEntry referenceEntry);
bool IsCompatibleWithModuleReferences(ExtensionDescriptor extension, IEnumerable<ExtensionProbeEntry> references); ExtensionProbeEntry Probe(ExtensionDescriptor descriptor);
ExtensionEntry Load(ExtensionDescriptor descriptor); void ExtensionActivated(ExtensionLoadingContext ctx, ExtensionDescriptor extension);
void ExtensionDeactivated(ExtensionLoadingContext ctx, ExtensionDescriptor extension);
void ExtensionRemoved(ExtensionLoadingContext ctx, DependencyDescriptor dependency); void Monitor(ExtensionDescriptor extension, Action<IVolatileToken> monitor); /// <summary>
/// Return a list of references required to compile a component (e.g. a Razor or WebForm view)
/// depending on the given module.
/// Each reference can either be an assembly name or a file to pass to the
/// IBuildManager.GetCompiledAssembly() method (e.g. a module .csproj project file).
/// </summary>
IEnumerable<ExtensionCompilationReference> GetCompilationReferences(DependencyDescriptor dependency);
/// <summary>
/// Return the list of dependencies (as virtual path) of the given module.
/// If any of the dependency returned in the list is updated, a component depending
/// on the assembly produced for the module must be re-compiled.
/// For example, Razor or WebForms views needs to be recompiled when a dependency of a module changes.
/// </summary>
IEnumerable<string> GetVirtualPathDependencies(DependencyDescriptor dependency);
}

  从上面接口可以看到Loader有很多方法,它们具有什么功能?
  根据接口,可以将其方法分为以下几类:
  ● 拓展模块:
    ○ 探测:ExtensionProbeEntry Probe(ExtensionDescriptor descriptor);
    ○ 加载:ExtensionEntry Load(ExtensionDescriptor descriptor);
  ● 模块引用:
    ○ 探测:IEnumerable<ExtensionReferenceProbeEntry> ProbeReferences(ExtensionDescriptor extensionDescriptor);
    ○ 加载:Assembly LoadReference(DependencyReferenceDescriptor reference);
  ● 激活与停用:
    ○ 模块激活:void ExtensionActivated(ExtensionLoadingContext ctx, ExtensionDescriptor extension);
    ○ 模块停用:void ExtensionDeactivated(ExtensionLoadingContext ctx, ExtensionDescriptor extension);
    ○ 模块删除:void ExtensionRemoved(ExtensionLoadingContext ctx, DependencyDescriptor dependency);
    ○ 引用激活:void ReferenceActivated(ExtensionLoadingContext context, ExtensionReferenceProbeEntry referenceEntry);
    ○ 引用停用:void ReferenceDeactivated(ExtensionLoadingContext context, ExtensionReferenceProbeEntry referenceEntry);
  ● 监控:void Monitor(ExtensionDescriptor extension, Action<IVolatileToken> monitor);
  ● 编译需要的引用:IEnumerable<ExtensionCompilationReference> GetCompilationReferences(DependencyDescriptor dependency);
  ● 依赖路径支持:IEnumerable<string> GetVirtualPathDependencies(DependencyDescriptor dependency);  

  按照上面的分类,可以大致猜测Loader的使用过程:探测(模块和引用)---->加载(模块和引用)----->激活引用----->激活模块。然后其它接口用于管理拓展的启用和停用,以及编译时的一些辅助。

  从前一篇文章告诉我们的是Orchard的模块激活是以"Module.txt"文件作为输入然后输出一个System.Type的列表。之前通过Folder已经搜索了所有相关目录,解析每一个目录下的Module.txt或Theme.txt文件,生成了一个Extension Descriptor对象列表。
  现在Orchard将通过这些对象使用Loader来探测实际模块以及实际引用的相关信息:
  先放上代码(位于ExtensionLoaderCoordinator类型的CreateLoadingContext方法):

        Logger.Information("Probing extensions");
var availableExtensionsProbes1 = _parallelCacheContext
.RunInParallel(availableExtensions, extension =>
_loaders.Select(loader => loader.Probe(extension)).Where(entry => entry != null).ToArray())
.SelectMany(entries => entries)
.GroupBy(entry => entry.Descriptor.Id); var availableExtensionsProbes = _parallelCacheContext
.RunInParallel(availableExtensionsProbes1, g =>
new { Id = g.Key, Entries = SortExtensionProbeEntries(g, virtualPathModficationDates)})
.ToDictionary(g => g.Id, g => g.Entries, StringComparer.OrdinalIgnoreCase);
Logger.Information("Done probing extensions"); var deletedDependencies = previousDependencies
.Where(e => !availableExtensions.Any(e2 => StringComparer.OrdinalIgnoreCase.Equals(e2.Id, e.Name)))
.ToList(); // Collect references for all modules
Logger.Information("Probing extension references");
var references = _parallelCacheContext
.RunInParallel(availableExtensions, extension => _loaders.SelectMany(loader => loader.ProbeReferences(extension)).ToList())
.SelectMany(entries => entries)
.ToList();
Logger.Information("Done probing extension references");

  从上面代码可以看到它做了三件事:1、探测所有的拓展信息。2、根据路径修改时间对探测信息进行排序。3、探测所有引用信息。

探测拓展信息:

 .RunInParallel(availableExtensions, extension =>
_loaders.Select(loader => loader.Probe(extension)).Where(entry => entry != null).ToArray())

  根据代码可以看出,对于每一个可用拓展,需要通过所有loader的探测:

  1. CoreExtensionLoader:很直接的判断是否来至于Core目录下,然后直接组建ExtensionProbeEntry。

     public override ExtensionProbeEntry Probe(ExtensionDescriptor descriptor) {
if (Disabled)
return null; if (descriptor.Location == "~/Core") {
return new ExtensionProbeEntry {
Descriptor = descriptor,
Loader = this,
Priority = , // Higher priority because assemblies in ~/bin always take precedence
VirtualPath = "~/Core/" + descriptor.Id,
VirtualPathDependencies = Enumerable.Empty<string>(),
};
}
return null;
}

    2. PrecompiledExtensionLoader:根据Id(模块名称)获取程序集文件路径,并组建ExtensionProbeEntry。它的依赖列表也是当前程序集。

 ...
      Logger.Information("Probing for module '{0}'", descriptor.Id); var assemblyPath = GetAssemblyPath(descriptor);
if (assemblyPath == null)
return null; var result = new ExtensionProbeEntry {
Descriptor = descriptor,
Loader = this,
VirtualPath = assemblyPath,
VirtualPathDependencies = new[] { assemblyPath },
}; Logger.Information("Done probing for module '{0}'", descriptor.Id);
...

  3. DynamicExtensionLoader:与PrecompiledExtensionLoader相似,只不过这里获取的是模块的csproj文件。但是这里要注意的是它的依赖信息通过GetDependencies方法获取了相关的文件信息。

         Logger.Information("Probing for module '{0}'", descriptor.Id);

             string projectPath = GetProjectPath(descriptor);
if (projectPath == null)
return null; var result = new ExtensionProbeEntry {
Descriptor = descriptor,
Loader = this,
VirtualPath = projectPath,
VirtualPathDependencies = GetDependencies(projectPath).ToList(),
}; Logger.Information("Done probing for module '{0}'", descriptor.Id);
---
public ProjectFileDescriptor Parse(TextReader reader) {
var document = XDocument.Load(XmlReader.Create(reader));
return new ProjectFileDescriptor {
AssemblyName = GetAssemblyName(document),
SourceFilenames = GetSourceFilenames(document).ToArray(),
References = GetReferences(document).ToArray()
};
}
private static string GetAssemblyName(XDocument document) {
return document
.Elements(ns("Project"))
.Elements(ns("PropertyGroup"))
.Elements(ns("AssemblyName"))
.Single()
.Value;
} private static IEnumerable<string> GetSourceFilenames(XDocument document) {
return document
.Elements(ns("Project"))
.Elements(ns("ItemGroup"))
.Elements(ns("Compile"))
.Attributes("Include")
.Select(c => c.Value);
} private static IEnumerable<ReferenceDescriptor> GetReferences(XDocument document) {
var assemblyReferences = document
.Elements(ns("Project"))
.Elements(ns("ItemGroup"))
.Elements(ns("Reference"))
.Where(c => c.Attribute("Include") != null)
.Select(c => {
string path = null;
XElement attribute = c.Elements(ns("HintPath")).FirstOrDefault();
if (attribute != null) {
path = attribute.Value;
} return new ReferenceDescriptor {
SimpleName = ExtractAssemblyName(c.Attribute("Include").Value),
FullName = c.Attribute("Include").Value,
Path = path,
ReferenceType = ReferenceType.Library
};
}); var projectReferences = document
.Elements(ns("Project"))
.Elements(ns("ItemGroup"))
.Elements(ns("ProjectReference"))
.Attributes("Include")
.Select(c => new ReferenceDescriptor {
SimpleName = Path.GetFileNameWithoutExtension(c.Value),
FullName = Path.GetFileNameWithoutExtension(c.Value),
Path = c.Value,
ReferenceType = ReferenceType.Project
}); return assemblyReferences.Union(projectReferences);
}

  获取依赖信息,实际上就是解析csproj这个xml文件。

    4. RawThemeExtensionLoader:这个Loader比较特殊,它用于加载~/themes目录下没有代码的主题。如果存在csproj和bin目录下的程序集都会被忽略。

        // Temporary - theme without own project should be under ~/themes
if (descriptor.Location.StartsWith("~/Themes",StringComparison.InvariantCultureIgnoreCase)) {
string projectPath = _virtualPathProvider.Combine(descriptor.Location, descriptor.Id,
descriptor.Id + ".csproj"); // ignore themes including a .csproj in this loader
if ( _virtualPathProvider.FileExists(projectPath) ) {
return null;
} var assemblyPath = _virtualPathProvider.Combine(descriptor.Location, descriptor.Id, "bin",
descriptor.Id + ".dll"); // ignore themes with /bin in this loader
if ( _virtualPathProvider.FileExists(assemblyPath) )
return null; return new ExtensionProbeEntry {
Descriptor = descriptor,
Loader = this,
VirtualPath = descriptor.VirtualPath,
VirtualPathDependencies = Enumerable.Empty<string>(),
};
}

    5. ReferencedExtensionLoader:引用拓展比较特殊,因为引用拓展是直接被根项目引用的模块项目,这些模块当根项目编译时,它们也会被编译并放置到根项目的bin目录下。

           var assembly = _buildManager.GetReferencedAssembly(descriptor.Id);
if (assembly == null)
return null; var assemblyPath = _virtualPathProvider.Combine("~/bin", descriptor.Id + ".dll"); return new ExtensionProbeEntry {
Descriptor = descriptor,
Loader = this,
Priority = , // Higher priority because assemblies in ~/bin always take precedence
VirtualPath = assemblyPath,
VirtualPathDependencies = new[] { assemblyPath },
};

注:这里BulidManager中是通过一个DefaultAssemblyLoader来加载程序集的,DefaultAssemblyLoader类型自身维护了一个ConcurrentDictionary<string, Assembly> _loadedAssemblies字典,用于通过程序集的Short Name来存取程序集,如果程序集不存在时就通过程序集的Full Name、Short Name甚至通过程序集解析器来解析出程序集的名称,最后通过这些名称依次通过.net的Assembly调用Load(name)的方法加载返回并对结果进行缓存。
Orchard中有三种名称解析器:AppDomainAssemblyNameResolver、OrchardFrameworkAssemblyNameResolver以及GacAssemblyNameResolver。 它们分别从AppDomain、OrchardFramework依赖的DefaultAssemblyLoader中以及.Net全局程序集中查找程序集。

  从上面的分析可知,它主要的操作是通过ExtensionDescriptor来获取对应模块的程序集路径和优先级,并以当前的加载器打包,以便后续用于加载对应程序集。最终ExtensionProbeEntry是以描述Id进行分组的,意味这同一个拓展存在多个探测实体(来自于不同Loader,主要是有不同的优先级以及路径修改时间)。

探测信息排序:

  这里主要是将上面获取的ExtensionProbeEntry,针对每一个Key(即ExtensionDescriptor.Id)通过优先级来分组,然后取出优先级最高的组,如果改组中存在多个项(如预编译加载器和动态加载器都探测到相应模块,且它们优先级一致)时,根据它们依赖路径的最新修改时间再次进行排序(上面创建ExtensionProbeEntry时VirtualPathDependencies一般为程序集路径,但是动态加载器是所有依赖文件路径,句话说只要任意文件的修改日期大于编译库的信息,那么动态加载器对应的探测实体就会排在前面优先使用)。

         private IEnumerable<ExtensionProbeEntry> SortExtensionProbeEntries(IEnumerable<ExtensionProbeEntry> entries, ConcurrentDictionary<string, DateTime> virtualPathModficationDates) {
// All "entries" are for the same extension ID, so we just need to filter/sort them by priority+ modification dates.
var groupByPriority = entries
.GroupBy(entry => entry.Priority)
.OrderByDescending(g => g.Key); // Select highest priority group with at least one item
var firstNonEmptyGroup = groupByPriority.FirstOrDefault(g => g.Any()) ?? Enumerable.Empty<ExtensionProbeEntry>(); // No need for further sorting if only 1 item found
if (firstNonEmptyGroup.Count() <= )
return firstNonEmptyGroup; // Sort by last modification date/loader order
return firstNonEmptyGroup
.OrderByDescending(probe => GetVirtualPathDepedenciesModificationTimeUtc(virtualPathModficationDates, probe))
.ThenBy(probe => probe.Loader.Order)
.ToList();
}
private DateTime GetVirtualPathDepedenciesModificationTimeUtc(ConcurrentDictionary<string, DateTime> virtualPathDependencies, ExtensionProbeEntry probe) {
if (!probe.VirtualPathDependencies.Any())
return DateTime.MinValue; Logger.Information("Retrieving modification dates of dependencies of extension '{0}'", probe.Descriptor.Id); var result = probe.VirtualPathDependencies.Max(path => GetVirtualPathModificationTimeUtc(virtualPathDependencies, path)); Logger.Information("Done retrieving modification dates of dependencies of extension '{0}'", probe.Descriptor.Id);
return result;
} private DateTime GetVirtualPathModificationTimeUtc(ConcurrentDictionary<string, DateTime> virtualPathDependencies, string path) {
return virtualPathDependencies.GetOrAdd(path, p => _virtualPathProvider.GetFileLastWriteTimeUtc(p));
}
public virtual DateTime GetFileLastWriteTimeUtc(string virtualPath) {
#if true
// This code is less "pure" than the code below, but performs fewer file I/O, and it
// has been measured to make a significant difference (4x) on slow file systems.
return File.GetLastWriteTimeUtc(MapPath(virtualPath));
#else
var dependency = HostingEnvironment.VirtualPathProvider.GetCacheDependency(virtualPath, new[] { virtualPath }, DateTime.UtcNow);
if (dependency == null) {
throw new Exception(string.Format("Invalid virtual path: '{0}'", virtualPath));
}
return dependency.UtcLastModified;
#endif
}

探测引用:

  引用探测是探测阶段的最后一步,它将查找所有模块的引用信息。

 .RunInParallel(availableExtensions, extension => _loaders.SelectMany(loader => loader.ProbeReferences(extension)).ToList())

      1. CoreExtensionLoader:基类的ProbeReferences方法将被调用,返回一个空集合。

      public virtual IEnumerable<ExtensionReferenceProbeEntry> ProbeReferences(ExtensionDescriptor descriptor) {
return Enumerable.Empty<ExtensionReferenceProbeEntry>();
}

      2. DynamicExtensionLoader:通过解析csproj文件获取的reference信息,创建ExtensionReferenceProbeEntry。

         public override IEnumerable<ExtensionReferenceProbeEntry> ProbeReferences(ExtensionDescriptor descriptor) {
if (Disabled)
return Enumerable.Empty<ExtensionReferenceProbeEntry>(); Logger.Information("Probing references for module '{0}'", descriptor.Id); string projectPath = GetProjectPath(descriptor);
if (projectPath == null)
return Enumerable.Empty<ExtensionReferenceProbeEntry>(); var projectFile = _projectFileParser.Parse(projectPath); var result = projectFile.References.Select(r => new ExtensionReferenceProbeEntry {
Descriptor = descriptor,
Loader = this,
Name = r.SimpleName,
VirtualPath = _virtualPathProvider.GetProjectReferenceVirtualPath(projectPath, r.SimpleName, r.Path)
}); Logger.Information("Done probing references for module '{0}'", descriptor.Id);
return result;
}

     3. PrecompiledExtensionLoader:找到模块程序集路径,将该路径下的所有除了模块程序集以外的.dll文件全部获取路径,并创建ExtensionReferenceProbeEntry。

         public override IEnumerable<ExtensionReferenceProbeEntry> ProbeReferences(ExtensionDescriptor descriptor) {
if (Disabled)
return Enumerable.Empty<ExtensionReferenceProbeEntry>(); Logger.Information("Probing references for module '{0}'", descriptor.Id); var assemblyPath = GetAssemblyPath(descriptor);
if (assemblyPath == null)
return Enumerable.Empty<ExtensionReferenceProbeEntry>(); var result = _virtualPathProvider
.ListFiles(_virtualPathProvider.GetDirectoryName(assemblyPath))
.Where(s => StringComparer.OrdinalIgnoreCase.Equals(Path.GetExtension(s), ".dll"))
.Where(s => !StringComparer.OrdinalIgnoreCase.Equals(Path.GetFileNameWithoutExtension(s), descriptor.Id))
.Select(path => new ExtensionReferenceProbeEntry {
Descriptor = descriptor,
Loader = this,
Name = Path.GetFileNameWithoutExtension(path),
VirtualPath = path
} )
.ToList(); Logger.Information("Done probing references for module '{0}'", descriptor.Id);
return result;
}

    4. RawThemeExtensionLoader:使用基类方法,获取空列表。
    5. ReferencedExtensionLoader:使用基类方法,获取空列表。

  这里要说明一下,因为Core模块和被引用的模块是被根目录直接引用的,所以在编译时会将模块所依赖的库一并复制到根项目的bin目录中,所以无需额外处理它们。预编译和动态编译的模块的引用信息分别从bin目录下或csproj文件中提取。
引用后续的处理就是根据模块和引用名称进行了分组,以便后续使用。

小结:以上过程主要目的是为了创建一个ExtensionLoadingContext用于后续加载。

 return new ExtensionLoadingContext {
AvailableExtensions = sortedAvailableExtensions,
PreviousDependencies = previousDependencies,
DeletedDependencies = deletedDependencies,
AvailableExtensionsProbes = availableExtensionsProbes,
ReferencesByName = referencesByName,
ReferencesByModule = referencesByModule,
VirtualPathModficationDates = virtualPathModficationDates,
}