Skip to content

Instantly share code, notes, and snippets.

@loic-sharma
Created February 7, 2021 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loic-sharma/d29a61396fdbeb05d40fdd7f099dca54 to your computer and use it in GitHub Desktop.
Save loic-sharma/d29a61396fdbeb05d40fdd7f099dca54 to your computer and use it in GitHub Desktop.
<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>
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