Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dependency Injection in a DotNetCore Console Application
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;
}
}
}
{
"MySettings": {
"IsCheesy": true,
"Name": "Developer Man"
}
}
{
"MySettings": {
"IsCheesy": false,
"Name": "George"
}
}
<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>
using System;
namespace MyConsoleApplication
{
public class MySettings
{
public bool IsCheesy { get; set; }
public string Name { get; set; }
}
}
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