private void Application_Startup(object sender, StartupEventArgs e)
        {
            string msgBadDataProviderConfig = $"Please specify a DATAPROVIDER configuration value of 'API', 'JSON', or 'MOCK'";
            var    dataProvider             = ConfigurationManager.AppSettings[CONFIG_DATAPROVIDER];

            if (string.IsNullOrWhiteSpace(dataProvider))
            {
                throw new ApplicationException(msgBadDataProviderConfig);
            }

            HideConsoleWindow();
            IServiceCollection services = Program.ServiceCollection;

            Log.Logger.Information($"Using {dataProvider} as a data provider.");
            switch (dataProvider?.ToUpperInvariant())
            {
            case "API":
                var baseUrl    = ConfigurationManager.AppSettings[CONFIG_BASEURL];
                var httpClient = new HttpClient
                {
                    BaseAddress = new Uri($"{baseUrl}")
                };
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Add("api-version", "1");
                httpClient.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                IServiceProvider serviceProvider = Program.ServiceProvider;
                var logger         = serviceProvider.GetService <ILogger <CitySelectorApiService> >();
                var apiDataService = new WebApiDataServiceCSC(log: logger, httpClient: httpClient);

                var citySelectorApiService = new CitySelectorApiService(logger, apiDataService);
                services.Replace(ServiceDescriptor.Singleton <ICitySelectorService>(_ => citySelectorApiService));
                break;

            default:
                throw new ApplicationException(msgBadDataProviderConfig);
                // break;
            }

            Program.ServiceProvider = services.BuildServiceProvider();
            var myWindow = Program.ServiceProvider.GetRequiredService <MainWindow>();

            myWindow.Show();
        }
Example #2
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            string msgBadDataProviderConfig = $"Please specify a DATAPROVIDER configuration value of 'API', 'JSON', or 'MOCK'";
            var    dataProvider             = ConfigurationManager.AppSettings[CONFIG_DATAPROVIDER];

            if (string.IsNullOrWhiteSpace(dataProvider))
            {
                throw new ApplicationException(msgBadDataProviderConfig);
            }

            HideConsoleWindow();
            IServiceCollection services = Program.ServiceCollection;

            Log.Logger.Information($"Using {dataProvider} as a data provider.");
            switch (dataProvider?.ToUpperInvariant())
            {
            case "API":
                var baseUrl = ConfigurationManager.AppSettings[CONFIG_BASEURL];
                IServiceProvider serviceProvider = Program.ServiceProvider;
                var logger = serviceProvider.GetService <ILogger <CitySelectorApiService> >();
                var citySelectorApiService = new CitySelectorApiService(logger, baseUrl);
                services.Replace(ServiceDescriptor.Singleton <ICitySelectorService>(_ => citySelectorApiService));
                break;

            case "JSON":
                services.Replace(ServiceDescriptor.Singleton <ICitySelectorService, CitySelectorJsonService>());
                break;

            case "MOCK":
                services.Replace(ServiceDescriptor.Singleton <ICitySelectorService, CitySelectorMockService>());
                break;

            default:
                throw new ApplicationException(msgBadDataProviderConfig);
                // break;
            }

            Program.ServiceProvider = services.BuildServiceProvider();
            var myWindow = Program.ServiceProvider.GetRequiredService <MainWindow>();

            myWindow.Show();
        }