How to run .NET Core Console app using generic host builder

EDIT: An update for .NET 6 is below ↓ Not much has changed with .NET 7. I’d start off with the default worker template. It comes with necessary packages pre-installed. If you already have a project, install Microsoft.Extensions.Hosting package. dotnet new worker -n MyCli Then open up the Program.cs and build the host. Remove the … Read more

How is a Scoped service instance handled in a .NET Core Console application?

When you build IServiceProvider from IServiceCollection (BuildServiceProvider method) and you use this instance of IServiceProvider to resolve IDbConnection you will get same instance of IDbConnection every time. Scope is connected to IServiceProvider. to create new scope you need to resolve from the container IServiceScopeFactory and use it to create IServiceProvider that is scoped: using (var … Read more

How can I read the appsettings.json in a .NET 6 console application?

Add these two nuget packages in your application Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.Json Then you can use ConfigurationBuilder to use appsettings.json file var configuration = new ConfigurationBuilder() .AddJsonFile($”appsettings.json”); var config = configuration.Build(); var connectionString = config.GetConnectionString(“ConnectionString”); Getting Values from AppSettings in Console Application Adding AppSettings in .net core app

How does a Spring Boot console based application work?

You should have a standard loader: @SpringBootApplication public class MyDemoApplication { public static void main(String[] args) { SpringApplication.run(MyDemoApplication.class, args); } } and implement a CommandLineRunner interface with @Component annotation @Component public class MyRunner implements CommandLineRunner { @Override public void run(String… args) throws Exception { } } @EnableAutoConfiguration will do the usual SpringBoot magic. UPDATE: As … Read more

Use custom console for Visual Studio Console Application Debugging

You’ve mix up terms. The “Windows Console” is not a “cmd.exe”, but special “service” which implemented, for example of Win7, with “conhost.exe”. When you start any console application (does not matter cmd, powershell, or your own app) windows starts it in special environment, which may have visible console window. But it is always internal Windows … Read more