瞭解到了OrchardCore主要由兩個中介軟體(ModularTenantContainerMiddleware和ModularTenantRouterMiddleware)構成,下面開始瞭解ModularTenantContainerMiddleware中介軟體第一行程式碼。
瞭解asp.net core機制的都知道中介軟體是由建構函式和Invoke(或者InokeAsync)方法構成,建構函式不過就是個依賴注入的過程,Invoke也可以直接依賴注入,兩者的區別是建構函式時全域性的,Invoke只是當次請求,所以從Inovke開始瞭解!
public async Task Invoke(HttpContext httpContext) { // Ensure all ShellContext are loaded and available. await _shellHost.InitializeAsync(); var shellSettings = _runningShellTable.Match(httpContext); // We only serve the next request if the tenant has been resolved. if (shellSettings != null) { if (shellSettings.State == TenantState.Initializing) { httpContext.Response.Headers.Add(HeaderNames.RetryAfter, "10"); httpContext.Response.StatusCode = StatusCodes.Status503ServiceUnavailable; await httpContext.Response.WriteAsync("The requested tenant is currently initializing."); return; } // Makes 'RequestServices' aware of the current 'ShellScope'. httpContext.UseShellScopeServices(); var shellScope = await _shellHost.GetScopeAsync(shellSettings); // Holds the 'ShellContext' for the full request. httpContext.Features.Set(new ShellContextFeature { ShellContext = shellScope.ShellContext, OriginalPath = httpContext.Request.Path, OriginalPathBase = httpContext.Request.PathBase }); await shellScope.UsingAsync(scope => _next.Invoke(httpContext)); } }
現在從第一行程式碼_shellHost.InitializeAsync()開始看起。_shellHost是一個IShellHost單例,是類ShellHost的例項化,這個很明顯就是asp.net core自帶的依賴注入生成的(這是在註冊服務的時候已經配置好的,具體追蹤services.AddOrchardCms就很清晰了)。現在直接找ShellHost的InitializeAsync方法和PreCreateAndRegisterShellsAsync方法。
public async Task InitializeAsync() { if (!_initialized) { // Prevent concurrent requests from creating all shells multiple times await _initializingSemaphore.WaitAsync(); try { if (!_initialized) { await PreCreateAndRegisterShellsAsync(); } } finally { _initialized = true; _initializingSemaphore.Release(); } } } private async Task PreCreateAndRegisterShellsAsync() { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("Start creation of shells"); } // Load all extensions and features so that the controllers are registered in // 'ITypeFeatureProvider' and their areas defined in the application conventions. var features = _extensionManager.LoadFeaturesAsync(); // Is there any tenant right now? var allSettings = (await _shellSettingsManager.LoadSettingsAsync()).Where(CanCreateShell).ToArray(); var defaultSettings = allSettings.FirstOrDefault(s => s.Name == ShellHelper.DefaultShellName); var otherSettings = allSettings.Except(new[] { defaultSettings }).ToArray(); await features; // The 'Default' tenant is not running, run the Setup. if (defaultSettings?.State != TenantState.Running) { var setupContext = await CreateSetupContextAsync(defaultSettings); AddAndRegisterShell(setupContext); allSettings = otherSettings; } if (allSettings.Length > 0) { // Pre-create and register all tenant shells. foreach (var settings in allSettings) { AddAndRegisterShell(new ShellContext.PlaceHolder { Settings = settings }); }; } if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("Done pre-creating and registering shells"); } }
InitializeAsync從字面上看就是初始的過程ShellHost(這個不知道怎麼翻譯比較準確)的過程,從程式碼上看也是如此。
首先通過_initialized判斷是否初始化過(開始沒賦值肯定false),在初始化結束_initialized就會設定為true,往下幾行的finally裡面可以看到_initialized = true,因為ShellHost是單例,也就是啟動程式之後就會一直維持_initialized = true,簡單說就是啟動時會初始化一次ShellHost。
再往下看,await _initializingSemaphore.WaitAsync,這個上面也有註釋防止併發請求多次建立所有shells,這個開始我也不懂的時候直接理解為lock後面finally的_initializingSemaphore.Release就是lock結束,後面找了點資料試了下,其實就是隻有一個請求可以進入初始化,建構函式只允許一個請求(SemaphoreSlim _initializingSemaphore = new SemaphoreSlim(1)),之所以說請求時因為這是中介軟體裡面啊,每個請求都要通過中介軟體(這個得了解asp.net core中介軟體的原理),看了OrchardCore真給我帶來不少知識點,沒遇到這個我只知道多執行緒用lock鎖住,原來還可以有這樣的細節。
然後try裡又確認一次有沒初始化(判斷_initialized),為啥呢?我一臉懵逼,後面感覺好像可以接受,比較前面時有很多請求,說不定有個某個請求已經初始化ShellHost了呢,所以_initializingSemaphore.WaitAsync開始後又要確認一次。如果前面不先確認,那麼又全部只能一個請求進來,這就很不合理了。我快被繞暈了,真佩服開發者的大腦。
終於來到PreCreateAndRegisterShellsAsync方法了。_loggoer部分直接跳過,就是asp.net core的日誌記錄而已。OrchardCore是一個模組化多租戶的cms,模組化就體現在這裡。
var features = _extensionManager.LoadFeaturesAsync();
載入所有的功能,ExtensionManager(OrchardCore\OrchardCore\Extensions\ExtensionManager.cs)的LoadFeaturesAsync方法。
public async Task<IEnumerable<FeatureEntry>> LoadFeaturesAsync() { await EnsureInitializedAsync(); return _features.Values; }
確保初始化並返回FeatureEntry集合,也就是所有功能的集合。現在進入EnsureInitializedAsync方法看看:
private async Task EnsureInitializedAsync() { if (_isInitialized) { return; } await _semaphore.WaitAsync(); try { if (_isInitialized) { return; } var modules = _applicationContext.Application.Modules; var loadedExtensions = new ConcurrentDictionary<string, ExtensionEntry>(); // Load all extensions in parallel await modules.ForEachAsync((module) => { if (!module.ModuleInfo.Exists) { return Task.CompletedTask; } var manifestInfo = new ManifestInfo(module.ModuleInfo); var extensionInfo = new ExtensionInfo(module.SubPath, manifestInfo, (mi, ei) => { return _featuresProvider.GetFeatures(ei, mi); }); var entry = new ExtensionEntry { ExtensionInfo = extensionInfo, Assembly = module.Assembly, ExportedTypes = module.Assembly.ExportedTypes }; loadedExtensions.TryAdd(module.Name, entry); return Task.CompletedTask; }); var loadedFeatures = new Dictionary<string, FeatureEntry>(); // Get all valid types from any extension var allTypesByExtension = loadedExtensions.SelectMany(extension => extension.Value.ExportedTypes.Where(IsComponentType) .Select(type => new { ExtensionEntry = extension.Value, Type = type })).ToArray(); var typesByFeature = allTypesByExtension .GroupBy(typeByExtension => GetSourceFeatureNameForType( typeByExtension.Type, typeByExtension.ExtensionEntry.ExtensionInfo.Id)) .ToDictionary( group => group.Key, group => group.Select(typesByExtension => typesByExtension.Type).ToArray()); foreach (var loadedExtension in loadedExtensions) { var extension = loadedExtension.Value; foreach (var feature in extension.ExtensionInfo.Features) { // Features can have no types if (typesByFeature.TryGetValue(feature.Id, out var featureTypes)) { foreach (var type in featureTypes) { _typeFeatureProvider.TryAdd(type, feature); } } else { featureTypes = Array.Empty<Type>(); } loadedFeatures.Add(feature.Id, new CompiledFeatureEntry(feature, featureTypes)); } }; // Feature infos and entries are ordered by priority and dependencies. _featureInfos = Order(loadedFeatures.Values.Select(f => f.FeatureInfo)); _features = _featureInfos.ToDictionary(f => f.Id, f => loadedFeatures[f.Id]); // Extensions are also ordered according to the weight of their first features. _extensionsInfos = _featureInfos.Where(f => f.Id == f.Extension.Features.First().Id) .Select(f => f.Extension); _extensions = _extensionsInfos.ToDictionary(e => e.Id, e => loadedExtensions[e.Id]); _isInitialized = true; } finally { _semaphore.Release(); } }
前面判斷初始化,多請求方面跟之前的是一樣的,這裡我又不理解了,之前不是判斷過了嗎,難道是因為這是個Task的原因導致可能跳出到其它執行緒!這裡先跳過!
var modules = _applicationContext.Application.Modules;這裡是通過反射得到所有的模組,至於過程真是一言難盡啊,這裡卡了我好幾個星期才明白究竟是個怎樣的過程。
看下OrchardCore\OrchardCore.Abstractions\Modules\ModularApplicationContext.cs這個類
public interface IApplicationContext { Application Application { get; } } public class ModularApplicationContext : IApplicationContext { private readonly IHostEnvironment _environment; private readonly IEnumerable<IModuleNamesProvider> _moduleNamesProviders; private Application _application; private static readonly object _initLock = new object(); public ModularApplicationContext(IHostEnvironment environment, IEnumerable<IModuleNamesProvider> moduleNamesProviders) { _environment = environment; _moduleNamesProviders = moduleNamesProviders; } public Application Application { get { EnsureInitialized(); return _application; } } private void EnsureInitialized() { if (_application == null) { lock (_initLock) { if (_application == null) { _application = new Application(_environment, GetModules()); } } } } private IEnumerable<Module> GetModules() { var modules = new ConcurrentBag<Module>(); modules.Add(new Module(_environment.ApplicationName, true)); var names = _moduleNamesProviders .SelectMany(p => p.GetModuleNames()) .Where(n => n != _environment.ApplicationName) .Distinct(); Parallel.ForEach(names, new ParallelOptions { MaxDegreeOfParallelism = 8 }, (name) => { modules.Add(new Module(name, false)); }); return modules; } }
明顯就是初始化Application,然後返回Application,首先看EnsureInitialized方法,然後就直接看GetModules方法。
第一行var modules = new ConcurrentBag<Module>();又觸碰到我的知識盲點了,作為一個只會用List<T>的人,我真的完全不知道ConcurrentBag<T>是什麼,馬上msdn下,原來ConcurrentBag<T>也是個集合,但是它是執行緒安全的。ok,集合開始新增資料,首先把當前專案的名稱OrchardCore.Cms.Web給加進去(modules.Add(new Module(_environment.ApplicationName, true));)。然後看
var names = _moduleNamesProviders .SelectMany(p => p.GetModuleNames()) .Where(n => n != _environment.ApplicationName) .Distinct();
找到_moduleNamesProviders的例項(雖然是個集合,但是隻有一個類實現了)AssemblyAttributeModuleNamesProvider,開啟這個類,直接看建構函式。
public class AssemblyAttributeModuleNamesProvider : IModuleNamesProvider { private readonly List<string> _moduleNames; public AssemblyAttributeModuleNamesProvider(IHostEnvironment hostingEnvironment) { var assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName)); _moduleNames = assembly.GetCustomAttributes<ModuleNameAttribute>().Select(m => m.Name).ToList(); } public IEnumerable<string> GetModuleNames() { return _moduleNames; } }
明顯的反射,通過讀取當前程式的程式集hostingEnvironment.ApplicationName就是OrchardCore.Cms.Web,也就是獲取OrchardCore.Cms.Web.dll所有自定義ModuleName屬性的Name集合(看下ModuleNameAttribute可以明白就是建構函式傳入的那個字串)。編譯一下程式,用反編譯工具讀取OrchardCore.Cms.Web.dll(ModuleName哪裡來的下篇再說,這也是我一直不明白卡了好幾個星期的地方)可以看到如下的ModuleName
返回ModularApplicationContext繼續看,就是用linq呼叫GetModuleNames方法把List<string>抽象成集合IEnumerable<string>通過篩選唯一值Distinct返回(當然排除掉當前專案,畢竟前面已經加入過執行緒安全集合了modules)names,然後通過多執行緒方法加入前面提到那個執行緒安全集合modules。然後把執行緒安全集合返回。
var names = _moduleNamesProviders .SelectMany(p => p.GetModuleNames()) .Where(n => n != _environment.ApplicationName) .Distinct(); Parallel.ForEach(names, new ParallelOptions { MaxDegreeOfParallelism = 8 }, (name) => { modules.Add(new Module(name, false)); });
學c#第一個多執行緒類就是Parallel,因此很清楚就是限制最大8個執行緒(為啥要限制最大8個執行緒,難道作者是用老i7那種4核8執行緒那種?),把names分別再多個執行緒傳遞給Module例項化然後加入執行緒安全集合modules返回出去給Application做建構函式的是引數例項化,這也是為啥要用執行緒安全集合的原因吧(我真哭了,像我這種菜鳥估計只會List<T>,然後foreach這種低效率的單執行緒方法了)。
ok,貼下Application類
public class Application { private readonly Dictionary<string, Module> _modulesByName; private readonly List<Module> _modules; public const string ModulesPath = "Areas"; public const string ModulesRoot = ModulesPath + "/"; public const string ModuleName = "Application Main Feature"; public const string ModuleDescription = "Provides components defined at the application level."; public static readonly string ModulePriority = int.MinValue.ToString(); public const string ModuleCategory = "Application"; public const string DefaultFeatureId = "Application.Default"; public const string DefaultFeatureName = "Application Default Feature"; public const string DefaultFeatureDescription = "Adds a default feature to the application's module."; public Application(IHostEnvironment environment, IEnumerable<Module> modules) { Name = environment.ApplicationName; Path = environment.ContentRootPath; Root = Path + '/'; ModulePath = ModulesRoot + Name; ModuleRoot = ModulePath + '/'; Assembly = Assembly.Load(new AssemblyName(Name)); _modules = new List<Module>(modules); _modulesByName = _modules.ToDictionary(m => m.Name, m => m); } public string Name { get; } public string Path { get; } public string Root { get; } public string ModulePath { get; } public string ModuleRoot { get; } public Assembly Assembly { get; } public IEnumerable<Module> Modules => _modules; public Module GetModule(string name) { if (!_modulesByName.TryGetValue(name, out var module)) { return new Module(string.Empty); } return module; } }
前面獲取所有模組也就是Modules屬性可以清晰的從建構函式那裡看到就是我們剛剛返回的執行緒安全集合modules,繞來繞去我都快被繞暈了,真想一個指標指過去。好了,繼續回到我們的初始化功能,就是ExtensionManager的EnsureInitializedAsync方法
private async Task EnsureInitializedAsync() { if (_isInitialized) { return; } await _semaphore.WaitAsync(); try { if (_isInitialized) { return; } var modules = _applicationContext.Application.Modules; var loadedExtensions = new ConcurrentDictionary<string, ExtensionEntry>(); // Load all extensions in parallel await modules.ForEachAsync((module) => { if (!module.ModuleInfo.Exists) { return Task.CompletedTask; } var manifestInfo = new ManifestInfo(module.ModuleInfo); var extensionInfo = new ExtensionInfo(module.SubPath, manifestInfo, (mi, ei) => { return _featuresProvider.GetFeatures(ei, mi); }); var entry = new ExtensionEntry { ExtensionInfo = extensionInfo, Assembly = module.Assembly, ExportedTypes = module.Assembly.ExportedTypes }; loadedExtensions.TryAdd(module.Name, entry); return Task.CompletedTask; }); var loadedFeatures = new Dictionary<string, FeatureEntry>(); // Get all valid types from any extension var allTypesByExtension = loadedExtensions.SelectMany(extension => extension.Value.ExportedTypes.Where(IsComponentType) .Select(type => new { ExtensionEntry = extension.Value, Type = type })).ToArray(); var typesByFeature = allTypesByExtension .GroupBy(typeByExtension => GetSourceFeatureNameForType( typeByExtension.Type, typeByExtension.ExtensionEntry.ExtensionInfo.Id)) .ToDictionary( group => group.Key, group => group.Select(typesByExtension => typesByExtension.Type).ToArray()); foreach (var loadedExtension in loadedExtensions) { var extension = loadedExtension.Value; foreach (var feature in extension.ExtensionInfo.Features) { // Features can have no types if (typesByFeature.TryGetValue(feature.Id, out var featureTypes)) { foreach (var type in featureTypes) { _typeFeatureProvider.TryAdd(type, feature); } } else { featureTypes = Array.Empty<Type>(); } loadedFeatures.Add(feature.Id, new CompiledFeatureEntry(feature, featureTypes)); } }; // Feature infos and entries are ordered by priority and dependencies. _featureInfos = Order(loadedFeatures.Values.Select(f => f.FeatureInfo)); _features = _featureInfos.ToDictionary(f => f.Id, f => loadedFeatures[f.Id]); // Extensions are also ordered according to the weight of their first features. _extensionsInfos = _featureInfos.Where(f => f.Id == f.Extension.Features.First().Id) .Select(f => f.Extension); _extensions = _extensionsInfos.ToDictionary(e => e.Id, e => loadedExtensions[e.Id]); _isInitialized = true; } finally { _semaphore.Release(); } }
現在modules有了,下行是var loadedExtensions = new ConcurrentDictionary<string, ExtensionEntry>();經過剛剛的執行緒安全集合,ConcurrentDictionary雖然接觸不多也明白這個是執行緒安全字典(學依賴注入的時候遇到過)。既然是執行緒安全字典,下面肯定又是多執行緒來填充這個執行緒安全字典loadedExtensions,這裡用了一個ForEachAsync的擴充套件方法分配多個任務,把每個modules的ManifestInfo分析出來的功能加入ConcurrentDictionary。具體細說估計篇幅太長了,先簡單介紹下吧。之前modules就是OrchardCore.Modules、OrchardCore.Modules.Cms和OrchardCore.Themes這三個解決方案下的專案集合,我們稱作模組集合,每個模組由一個或者多個功能組成,現在就是把功能封裝成執行緒安全的字典集合,而功能是有重複的,可能多個模組用同一個功能,所以執行緒安全很重要!下篇要解釋下反射那些ModuleName是如何實現的(畢竟困擾我這麼久的東西必須記錄下才能印象深刻),我看下下篇能不能把功能拆出來說吧。