Created
December 10, 2022 23:02
-
-
Save icodeintx/a895275c6547df4b5d29efab308508d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# nullable disable | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
var host = CreateHostBuilder(args).Build(); | |
Application app = host.Services.GetRequiredService<Application>(); | |
static IHostBuilder CreateHostBuilder(string[] args) | |
{ | |
return Host.CreateDefaultBuilder(args) | |
.ConfigureServices( | |
(_, services) => services | |
.AddSingleton<Application, Application>() | |
.AddSingleton<IAppConfig, AppConfig>()); | |
} | |
interface IAppConfig | |
{ | |
string Setting { get; } | |
} | |
class AppConfig : IAppConfig | |
{ | |
public string Setting { get; } | |
public AppConfig() | |
{ | |
Console.WriteLine("AppConfig constructed"); | |
} | |
} | |
class Application | |
{ | |
readonly IAppConfig config; | |
public Application(IAppConfig config, ILogger<Application> logger) | |
{ | |
this.config = config; | |
logger.Log(LogLevel.Information, "Application constructed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment