Skip to content

Instantly share code, notes, and snippets.

@adamstirtan
Created October 26, 2023 14:07
Show Gist options
  • Select an option

  • Save adamstirtan/317da06d1874df04a4fbb172c9750c6e to your computer and use it in GitHub Desktop.

Select an option

Save adamstirtan/317da06d1874df04a4fbb172c9750c6e to your computer and use it in GitHub Desktop.
.NET Console app with dependency injection
using Microsoft.Extensions.DependencyInjection
namespace MyApp
{
internal class Program
{
private static void Main(string[] args)
{
var services = CreateServices();
Application app = services.GetRequiredService<Application>();
app.MyLogic();
}
private static ServiceProvider CreateServices()
{
var serviceProvider = new ServiceCollection()
.AddSingleton<Application>(new Application())
.BuildServiceProvider();
return serviceProvider;
}
}
public class Application
{
public void MyLogic()
{
// Do something epic
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment