Created
October 26, 2023 14:07
-
-
Save adamstirtan/317da06d1874df04a4fbb172c9750c6e to your computer and use it in GitHub Desktop.
.NET Console app with dependency injection
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
| 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