Created
November 22, 2018 03:00
-
-
Save fooberichu150/5d531c34644ff89687fad53b5fdba5f5 to your computer and use it in GitHub Desktop.
Dependency Injection in a DotNetCore Console Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.Extensions.Options; | |
namespace MyConsoleApplication | |
{ | |
public class Application | |
{ | |
private readonly IOptions<MySettings> _settings = null; | |
public Application(IOptions<MySettings> mySettings) | |
{ | |
// you can optionally do .Value but that makes it so updates in the JSON file won't reload in this instance | |
_settings = mySettings; | |
} | |
protected MySettings Settings => _settings.Value; | |
public bool Run() | |
{ | |
if (!string.IsNullOrWhiteSpace(Settings.Name)) | |
Console.WriteLine($"Hello {Settings.Name}."); | |
Console.WriteLine($"This application is {(Settings.IsCheesy ? "Cheesy" : "Not Cheesy")}"); | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"MySettings": { | |
"IsCheesy": true, | |
"Name": "Developer Man" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"MySettings": { | |
"IsCheesy": false, | |
"Name": "George" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.1</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<Content Include="appsettings.Development.json"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
</Content> | |
<Content Include="appsettings.json"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
</Content> | |
</ItemGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MyConsoleApplication | |
{ | |
public class MySettings | |
{ | |
public bool IsCheesy { get; set; } | |
public string Name { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace MyConsoleApplication | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
try | |
{ | |
string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | |
if (string.IsNullOrWhiteSpace(env)) | |
{ | |
env = "Development"; | |
} | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true) | |
.AddEnvironmentVariables(); | |
IConfigurationRoot configuration = builder.Build(); | |
var services = new ServiceCollection(); | |
services.AddTransient<Application>(); | |
services.Configure<MySettings>(configuration.GetSection("MySettings")); | |
var provider = services.BuildServiceProvider(); | |
var application = provider.GetService<Application>(); | |
application.Run(); | |
} | |
finally | |
{ | |
Console.WriteLine("\r\nPress any key to exit..."); | |
Console.ReadKey(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment