From d896e47291c7e1b21c8ba43b63a154eaa988ddc1 Mon Sep 17 00:00:00 2001 From: Luke Malpass Date: Sat, 16 Jun 2018 15:32:56 +0100 Subject: [PATCH] Example of using .Net Core DI outside of ASP.Net Core --- DotNetCoreDependencyInjection/ConsoleApp1.sln | 25 ++++++ .../ConsoleApp1/ConsoleApp1.csproj | 15 ++++ .../ConsoleApp1/Program.cs | 83 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 DotNetCoreDependencyInjection/ConsoleApp1.sln create mode 100644 DotNetCoreDependencyInjection/ConsoleApp1/ConsoleApp1.csproj create mode 100644 DotNetCoreDependencyInjection/ConsoleApp1/Program.cs diff --git a/DotNetCoreDependencyInjection/ConsoleApp1.sln b/DotNetCoreDependencyInjection/ConsoleApp1.sln new file mode 100644 index 0000000..74e4c3e --- /dev/null +++ b/DotNetCoreDependencyInjection/ConsoleApp1.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2026 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{3B65C372-E1B4-412C-BB31-6B319B7C2402}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3B65C372-E1B4-412C-BB31-6B319B7C2402}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3B65C372-E1B4-412C-BB31-6B319B7C2402}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3B65C372-E1B4-412C-BB31-6B319B7C2402}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3B65C372-E1B4-412C-BB31-6B319B7C2402}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A95F76B1-5DE1-43C6-9B9B-5E9F8D1928FE} + EndGlobalSection +EndGlobal diff --git a/DotNetCoreDependencyInjection/ConsoleApp1/ConsoleApp1.csproj b/DotNetCoreDependencyInjection/ConsoleApp1/ConsoleApp1.csproj new file mode 100644 index 0000000..eb31f6f --- /dev/null +++ b/DotNetCoreDependencyInjection/ConsoleApp1/ConsoleApp1.csproj @@ -0,0 +1,15 @@ + + + + Exe + netcoreapp2.0 + + + + + + + + + + diff --git a/DotNetCoreDependencyInjection/ConsoleApp1/Program.cs b/DotNetCoreDependencyInjection/ConsoleApp1/Program.cs new file mode 100644 index 0000000..9e136c3 --- /dev/null +++ b/DotNetCoreDependencyInjection/ConsoleApp1/Program.cs @@ -0,0 +1,83 @@ +using Dna; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System; + +namespace ConsoleApp1 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + + // Add Microsoft.Extensions.DependencyInjection + // Microsoft.Extensions.Configuration(for ConfigurationBuilder) + // Microsoft.Extensions.Configuration.Json(for AddJsonFile) + + // + // So its your job to distribute the service collection to any add-in, + // library or part of your code that needs to add its own dependecies + // + // If using configuration you should also pass that along + // + // Then once done you build the service collection into a service provider + // which is now your source of dependency injection where all of your code + // can get services from the provider.GetService<> + // + // So typically this provider is a static instance in a core library + // so all of your code can access it + // + + // Create a new list of dependencies + var services = new ServiceCollection(); + + // At this point, all dependencies can be added to the DI system via the service collection + + // Configurations are used heavily in the .Net Core DI for configuring services + // So we can make use of that + // Create our configuration sources + var configurationBuilder = new ConfigurationBuilder(); + // Add environment variables + //.AddEnvironmentVariables(); + + // Add application settings json files + configurationBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); + + // Build configuration + var configuration = configurationBuilder.Build(); + + // Inject configuration into services + services.AddSingleton(configuration); + + // Build provider + var provider = services.BuildServiceProvider(); + + // After this point, DI is available and through the provider + + + // + // Dna framework + // + // + // Use Framework.Construct for totally blank service provider + // containing just the FrameworkEnvironment service, no Configuration or anything else + + // Use the DefaultFrameworkConstruction to add a Configuration similar to ASP.Net + // with the Configuration source of appsettings.json file, and to also add + // a basic console/debug logger and an exception handler that just logs errors + Framework.Construct() + // Add further services like this + .AddFileLogger() + // And once done build + .Build(); + + // Now the service provider is here + Framework.Provider.GetService().LogCriticalSource("Some important message"); + + // Or shortcuts here + FrameworkDI.Logger.LogCriticalSource("Shortcut to important message"); + } + } +}