Exemplo n.º 1
0
        protected virtual IApp InitAppInstance(Xml.AppInfo appInfo, PathMap appPathMap, IConfigurationRoot appConfiguration)
        {
            // Load types described in the application XML file
            var appType = this.LoadTypeByName(appInfo.App.Type);
            var modules = new AppModuleCollection();

            foreach (var module in appInfo.Modules)
            {
                if (module.Environment != null && !module.Environment.Equals(this.EnvName, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                modules.Add(this.LoadTypeByName(module.Type));
            }

            var startupServices = new StartupServiceCollection();

            foreach (var startupService in appInfo.StartupServices)
            {
                if (startupService.Environment != null && !startupService.Environment.Equals(this.EnvName, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                startupServices.Add(this.LoadTypeByName(startupService.Type));
            }

            // Initialize service container required to build and run an application
            var services = new ServiceCollection();

            services.AddTransient(appType);
            services.AddSingleton(provider => new AppIdentity(appInfo.App.Name, this.AppKind));
            services.AddSingleton(provider => new AppEnvironment(this.EnvName, appPathMap));
            services.AddSingleton(provider => appConfiguration);
            services.AddSingleton <IAppModuleCollection>(provider => modules);
            services.AddSingleton <IStartupServiceCollection>(provider => startupServices);
            services.AddTransient <AppRunnerModule>();
            if (PlatformServices.Default != null)
            {
                services.AddSingleton(provider => PlatformServices.Default);
            }

            var container = services.BuildServiceProvider();

            // Inject runner module in the app and build the app
            if (!this.DisableRunnerModule)
            {
                modules.StageZero.Add(container.GetRequiredService <AppRunnerModule>());
            }

            // Create the app
            return((IApp)container.GetService(appType));
        }
Exemplo n.º 2
0
        protected virtual PathMap CreateAppPathMap(Xml.AppInfo appInfo, PathMap basePathMap)
        {
            var appPathMaps = new Dictionary <string, PathMap>();

            foreach (var pm in appInfo.PathMaps)
            {
                var appPathMap = new PathMap(pm.Name, pm.Prefix, basePathMap);
                appPathMap.Set("AppName", appInfo.App.Name);
                appPathMap.Set("AppKind", this.AppKind);
                appPathMap.Set("AppEnvironment", this.EnvName ?? string.Empty);

                foreach (var path in pm.Paths ?? new Xml.PathMapPathElement[0])
                {
                    var items = path.Items?.Select(element => element.Path) ?? new[] { path.Path };
                    appPathMap.Set(path.Name, items);
                }

                appPathMaps[appPathMap.Name] = appPathMap;
            }

            foreach (var pm in appInfo.PathMaps)
            {
                if (!string.IsNullOrEmpty(pm.Extends))
                {
                    var appPathMap = appPathMaps[pm.Name];
                    if (!appPathMaps.TryGetValue(pm.Extends, out var parentPathMap))
                    {
                        throw new Exception($"Can not find base PathMap with name \"{pm.Extends}\"");
                    }

                    appPathMap.Merge(parentPathMap);
                }
            }

            if (!appPathMaps.TryGetValue(this.PathMapName, out var currentPathMap))
            {
                throw new Exception($"Can not find path map \"{this.PathMapName}\".");
            }

            if (this.PathMapPrefix != null)
            {
                currentPathMap.Prefix = this.PathMapPrefix;
            }

            this.ApplyPathMapOverrides(currentPathMap);

            return(currentPathMap);
        }
Exemplo n.º 3
0
        protected virtual IConfigurationRoot LoadAppConfiguration(Xml.AppInfo appInfo, PathMap appPathMap)
        {
            var builder = new ConfigurationBuilder();

            foreach (var configuration in appInfo.ConfigurationFiles)
            {
                if (configuration.Environment != null && !configuration.Environment.Equals(this.EnvName, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (configuration is ConfigurationEnvironmentVariablesElement environmentVariablesConfiguration)
                {
                    builder.Add(new EnvironmentVariablesConfigurationSource()
                    {
                        Prefix = environmentVariablesConfiguration.Prefix,
                    });
                }
                else if (configuration is ConfigurationFileElement fileConfiguration)
                {
                    var loaded = false;
                    foreach (var configPath in appPathMap.Compile(fileConfiguration.Path))
                    {
                        if (File.Exists(configPath))
                        {
                            try
                            {
                                FileConfigurationSource source;
                                switch (fileConfiguration.Type)
                                {
                                case "ini": source = new IniConfigurationSource(); break;

                                case "xml": source = new XmlConfigurationSource(); break;

                                case "json": source = new JsonConfigurationSource(); break;

                                default: throw new Exception("Unknown configuration type.");
                                }

                                source.FileProvider   = new PhysicalFileProvider(Path.GetDirectoryName(configPath));
                                source.Path           = Path.GetFileName(configPath);
                                source.ReloadOnChange = fileConfiguration.ReloadOnChange;

                                builder.Add(source);
                            }
                            catch (Exception exception)
                            {
                                throw new Exception($"Can not read configuration file \"{configPath}\": {exception.Message}", exception);
                            }

                            loaded = true;
                            break;
                        }
                    }

                    if (!fileConfiguration.Optional && !loaded)
                    {
                        throw new Exception($"Can not find required configuration file \"{fileConfiguration.Path}\".");
                    }
                }
            }

            return(builder.Build());
        }