I have a self-hosted .NET Core Console Application.
The web shows examples for ASP.NET Core but i do not have a webserver. Just a simple command line application.
Is it possible to do something like this for console applications?
public static void Main(string[] args)
{
// I don't want a WebHostBuilder. Just a command line
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
I would like to use a Startup.cs like in ASP.NET Core but on console.
How to this?
Yes, it is. ASP.NET Core applications can either be self-hosted - as in your example - or hosted inside a web server such as IIS. In .NET Core all apps are console apps.
All
.NET Core
applications are composed of well-crafted independent libraries and packages which you're free to reference and use in any type of application. It just so happens that anAsp.net core
application comes preconfigured to reference a lot of those libraries and exposes an http endpoint.But if it's Dependency Injection you need for your console app, simply reference the appropriate library. Here's a guide: http://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/
Another way would be using
HostBuilder
fromMicrosoft.Extensions.Hosting
package.So i came across with this solution, inspired by the accepted answer:
Program.cs
Startup.cs
appsettings.json
MyService.cs
IMyService.cs