From 6505d203c6d831400e9e0945a1c65ec0938bd915 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Tue, 2 Jul 2019 12:48:30 -0500 Subject: [PATCH] tidy DI registration --- .../Extensions/ServiceCollectionExtensions.cs | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs index 536d8e02e..ecc2cca1d 100644 --- a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs @@ -16,25 +16,11 @@ public static class ServiceCollectionExtensions public static void RegisterAgentServices(this IServiceCollection services) { services.AddConfiguration(); - services.AddLogging(b => - { - b.AddConsole(); - var appInsightsKey = b.Services.BuildServiceProvider().GetService>().Value - .InstrumentationKey; - b.AddApplicationInsights(appInsightsKey); - }); + services.AddLoggingCustom(); services.AddMediatR(typeof(Program).Assembly); services.AddHttpClient(); services.AddSingleton(); - services.AddSingleton(s => - { - var gitHubConfig = s.GetService>(); - var client = new GitHubClient(new ProductHeaderValue(gitHubConfig.Value.ProductHeaderValue)) - { - Credentials = new Credentials(gitHubConfig.Value.OauthToken) - }; - return client; - }); + services.AddGitHubClient(); services.AddTransient(); services.AddTransient(); } @@ -52,5 +38,27 @@ private static void AddConfiguration(this IServiceCollection services) services.Configure(config.GetSection(nameof(ConnectionStrings))); services.Configure(config.GetSection(nameof(GitHub))); } + + private static void AddLoggingCustom(this IServiceCollection services) + { + services.AddLogging(b => + { + b.AddConsole(); + var appInsightsConfig = b.Services.BuildServiceProvider().GetService>(); + b.AddApplicationInsights(appInsightsConfig.Value.InstrumentationKey); + }); + } + + private static void AddGitHubClient(this IServiceCollection services) + { + services.AddSingleton(s => + { + var gitHubConfig = s.GetService>(); + return new GitHubClient(new ProductHeaderValue(gitHubConfig.Value.ProductHeaderValue)) + { + Credentials = new Credentials(gitHubConfig.Value.OauthToken) + }; + }); + } } } \ No newline at end of file