Created
February 7, 2021 04:37
-
-
Save loic-sharma/d29a61396fdbeb05d40fdd7f099dca54 to your computer and use it in GitHub Desktop.
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>net5.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.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 System; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
// Create the generic host for my application. | |
IHost app = Host | |
.CreateDefaultBuilder(args) | |
.ConfigureServices(ConfigureServices) | |
.Build(); | |
// Use the dependency injection to grab my command handler and run it. | |
MyCommand command = app.Services.GetRequiredService<MyCommand>(); | |
command.Run(); | |
// Prepare my dependency injection container. | |
void ConfigureServices(HostBuilderContext context, IServiceCollection services) | |
{ | |
services.AddSingleton<MyCommand>(); | |
} | |
public class MyCommand | |
{ | |
public void Run() | |
{ | |
Console.WriteLine("Hello"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment