Last active
September 7, 2023 16:33
-
-
Save kshyju/0b9c517d4c434972c5d8556385408d5b to your computer and use it in GitHub Desktop.
ConsoleAppDI
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>net7.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" /> | |
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" /> | |
</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 Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
namespace ConsoleApp1 | |
{ | |
public interface IMyService | |
{ | |
string GetMessage(); | |
} | |
public class MyService : IMyService | |
{ | |
private IConfiguration? _configuration; | |
private ILogger<MyService> _logger; | |
public MyService(ILogger<MyService> logger) | |
{ | |
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | |
} | |
public MyService(IConfiguration configuration, ILogger<MyService> logger) | |
{ | |
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); | |
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | |
} | |
public string GetMessage() | |
{ | |
if (_configuration is not null) | |
{ | |
_logger.LogInformation($"Name: {_configuration["Name"]}"); | |
} | |
_logger.LogInformation("Inside GetMessage"); | |
return $"Hello {DateTime.Now}"; | |
} | |
} | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
IServiceProvider serviceProvider = BuildServiceProvider(); | |
var myService = serviceProvider.GetRequiredService<IMyService>(); | |
Console.WriteLine("Hello, World! " + myService.GetMessage()); | |
} | |
private static IServiceProvider BuildServiceProvider() | |
{ | |
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder() | |
.AddInMemoryCollection(new Dictionary<string, string?> { | |
{ "Name","foobar" } | |
}); | |
IConfiguration config = configurationBuilder.Build(); | |
IServiceProvider serviceProvider = new ServiceCollection() | |
.AddSingleton<IMyService, MyService>() | |
.AddSingleton<IConfiguration>(config) | |
.AddLogging(c => { c.AddConsole(); }) | |
.BuildServiceProvider(); | |
return serviceProvider; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment