Exemple #1
0
 protected internal virtual void ConfigureShellCreationScope(ShellCreationScope scope)
 {
 }
        private static void InitShell(SchubertServicesBuilder schubertBuilder, Action <ShellCreationScope> setup)
        {
            ShellCreationScope shellScope = new ShellCreationScope();

            setup?.Invoke(shellScope);

            var builder = schubertBuilder.ServiceCollection.FillToOther();

            builder.AddSmart(SchubertServices.GetServices(schubertBuilder.Configuration));
            builder.AddLogging(lb =>
            {
                shellScope.LoggingConfigure?.Invoke(lb);
            });

            var scopeFactory = builder.BuildServiceProvider().GetRequiredService <IServiceScopeFactory>();

            using (IServiceScope scope = scopeFactory.CreateScope())
            {
                IServiceProvider provider = scope.ServiceProvider;
                var shellLogger           = provider.GetRequiredService <ILoggerFactory>().CreateLogger("Schubert");

                shellLogger.WriteInformation("开始加载 Shell。");

                var sw = Stopwatch.StartNew();
                SchubertEngine.Current.LoadEnvironment(provider);
                var factory = provider.GetRequiredService <IShellContextFactory>();
                var context = factory.CreateShellContext();

                AddConfiguredOptions(schubertBuilder.ServiceCollection, schubertBuilder.Configuration, context);

                //微软框架依然存在解决日志组件创建后的销毁问题,只能通过移除日志来达到清理目的。
                schubertBuilder.ServiceCollection.Remove(sd => sd.ServiceType.Equals(typeof(ILoggerFactory)));
                schubertBuilder.ServiceCollection.AddLogging(b => b.AddConfiguration(schubertBuilder.Configuration.GetSection("Logging")));

                schubertBuilder.ServiceCollection.AddSingleton(context);
                schubertBuilder.ServiceCollection.AddSmart(context.Services);
                schubertBuilder.ServiceCollection.AddSmart(SchubertServices.GetServices(schubertBuilder.Configuration));

                SchubertEngine.Current.ShellCreated = true;

                IOptions <SchubertOptions> schubertOptions = provider.GetRequiredService <IOptions <SchubertOptions> >();

                context.RegisteredServices = builder;
                ShellEvents.NotifyShellInitialized(schubertOptions.Value, context);
                sw.Stop();


                var table = new Tuple <int, int, int, int, String>(
                    context.Blueprint.Modules.Count(),
                    context.Blueprint.Controllers.Count(),
                    context.Blueprint.Dependencies.Count(),
                    context.Blueprint.DependencyDescribers.Count(),
                    context.Blueprint.Modules.ToArrayString(System.Environment.NewLine));

                StringBuilder info = new StringBuilder();
                info.AppendLine(@"
 ________  ________  ___  ___  ___  ___  ________  _______   ________  _________   
|\   ____\|\   ____\|\  \|\  \|\  \|\  \|\   __  \|\  ___ \ |\   __  \|\___   ___\ 
\ \  \___|\ \  \___|\ \  \\\  \ \  \\\  \ \  \|\ /\ \   __/|\ \  \|\  \|___ \  \_| 
 \ \_____  \ \  \    \ \   __  \ \  \\\  \ \   __  \ \  \_|/_\ \   _  _\   \ \  \  
  \|____|\  \ \  \____\ \  \ \  \ \  \\\  \ \  \|\  \ \  \_|\ \ \  \\  \|   \ \  \ 
    ____\_\  \ \_______\ \__\ \__\ \_______\ \_______\ \_______\ \__\\ _\    \ \__\
   |\_________\|_______|\|__|\|__|\|_______|\|_______|\|_______|\|__|\|__|    \|__|
   \|_________|                                                                         
                                                                                   ");

                info.AppendLine($"Shell 加载完成,Schubert Version: {typeof(SchubertException).GetTypeInfo().Assembly.GetName().Version.ToString()} ({sw.ElapsedMilliseconds} ms)。");
                info.AppendLine("  ");
                info.AppendLine((new Tuple <int, int, int, int, String>[] { table }).ToStringTable(new String[] { "modules", "controllers", "dependencies", "describers", "moduleList" },
                                                                                                   t => t.Item1, t => t.Item2, t => t.Item3, t => t.Item4, t => t.Item5));
                info.AppendLine("   ");
                info.Append(context.Blueprint.Dependencies.GroupBy(d => d.Feature.Descriptor.ModuleName, d => (ShellBlueprintDependencyItem)d).ToStringTable(
                                new string[] { "module", "dependencies", "internfaces", "lifetime" },
                                f => f.Key,
                                d => d.SelectMany(t => CreateArray(t.Type.Name, t.Interfaces.Count)).ToArrayString(System.Environment.NewLine),
                                d => d.SelectMany(t => t.Interfaces).Select(i => i.Item1.Name).ToArrayString(System.Environment.NewLine),
                                d => d.SelectMany(t => t.Interfaces).Select(i => i.Item2.ToString().ToLower()).ToArrayString(System.Environment.NewLine)));
                info.AppendLine("   ");

                shellLogger.WriteInformation(info.ToString());
            }
        }