Example of using .Net Core DI outside of ASP.Net Core
This commit is contained in:
25
DotNetCoreDependencyInjection/ConsoleApp1.sln
Normal file
25
DotNetCoreDependencyInjection/ConsoleApp1.sln
Normal file
@ -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
|
||||||
15
DotNetCoreDependencyInjection/ConsoleApp1/ConsoleApp1.csproj
Normal file
15
DotNetCoreDependencyInjection/ConsoleApp1/ConsoleApp1.csproj
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Dna.Framework" Version="1.0.7.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
83
DotNetCoreDependencyInjection/ConsoleApp1/Program.cs
Normal file
83
DotNetCoreDependencyInjection/ConsoleApp1/Program.cs
Normal file
@ -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<IConfiguration>(configuration);
|
||||||
|
|
||||||
|
// Build provider
|
||||||
|
var provider = services.BuildServiceProvider();
|
||||||
|
|
||||||
|
// After this point, DI is available and through the provider
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Dna framework
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Use Framework.Construct<FrameworkConstruction> 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<DefaultFrameworkConstruction>()
|
||||||
|
// Add further services like this
|
||||||
|
.AddFileLogger()
|
||||||
|
// And once done build
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Now the service provider is here
|
||||||
|
Framework.Provider.GetService<ILogger>().LogCriticalSource("Some important message");
|
||||||
|
|
||||||
|
// Or shortcuts here
|
||||||
|
FrameworkDI.Logger.LogCriticalSource("Shortcut to important message");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user