private string WebSettingsFilePath(IFileProvider fileProvider) { string filePath = $"{WEB_SETTINGS_CATALOG_NAME}/{WEB_SETTINGS_FILE_NAME}"; IFileInfo fileInfo = fileProvider.GetFileInfo(WEB_SETTINGS_CATALOG_NAME); if (!fileInfo.Exists) { Directory.CreateDirectory(fileInfo.PhysicalPath); } fileInfo = fileProvider.GetFileInfo(filePath); if (!fileInfo.Exists) { WebSettings settings = new WebSettings(); JsonSerializerOptions options = new JsonSerializerOptions() { WriteIndented = true }; string json = JsonSerializer.Serialize(settings, options); using (StreamWriter writer = new StreamWriter(fileInfo.PhysicalPath, false, Encoding.UTF8)) { writer.Write(json); } } return(filePath); }
private void SetupServices(IConfiguration configuration, IServiceCollection services) { services.AddOptions(); services.Configure <AppSettings>(configuration.GetSection(nameof(AppSettings))); IFileProvider fileProvider = ConfigureFileProvider(services); WebSettings settings = ConfigureWebSettings(fileProvider, services); MetadataSettings metadataSettings = ConfigureMetadataSettings(fileProvider, services); services.AddTransient <TabViewModel>(); services.AddSingleton <MainWindowViewModel>(); services.AddSingleton <MetadataController>(); services.AddSingleton <ScriptingController>(); services.AddTransient <ScriptEditorViewModel>(); services.AddSingleton <HttpServicesController>(); services.AddSingleton <MessagingController>(); services.AddSingleton <IMetadataService, MetadataService>(); services.AddSingleton <IQueryExecutor, QueryExecutor>(); services.AddSingleton <IScriptingService, ScriptingService>(); services.AddSingleton <IMessagingService, MessagingService>(); services.AddHttpClient(); foreach (WebServer server in settings.WebServers) { services.AddHttpClient(server.Name, client => { client.BaseAddress = new Uri(server.Address); }); } }
private WebSettings ConfigureWebSettings(IFileProvider fileProvider, IServiceCollection services) { string filePath = WebSettingsFilePath(fileProvider); WebSettings settings = new WebSettings(); var config = new ConfigurationBuilder() .AddJsonFile(filePath, optional: false) .Build(); config.Bind(settings); services.Configure <WebSettings>(config); return(settings); }