C
C#9mo ago
M B V R K

How to hide the Console of a Worker Service application

I have made an application of type Worker Service. using .NET 8, The issue is I want that service to run without that console. Please is there any solution for that ????
2 Replies
SparkyCracked
SparkyCracked9mo ago
Normally the console is shown by default when running in debug mode, but when you deploy it, the console window shouldn't show. If it still shows after you deploy just check to see if its a console application or a Worker Service Application I can give you the steps to configure your worker service to run as a Windows Service. 1) First step is to configure your Worker service to run as a Window Service. You can do this using the Microsoft.Extensions.Hosting.WindowsServices package. You can get it using the NuGet Package manager or .NET CLI: dotnet add package Microsoft.Extensions.Hosting.WindowsServices 2) In your code under CreateHostBuilder method, in your Program.cs file, modify it to use UseWindowsService method:
c#
using Microsoft.Extensions.Hosting.WindowsServices;

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
}
c#
using Microsoft.Extensions.Hosting.WindowsServices;

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
}
And then just publish your application. You can use the terminal to do this: dotnet publish -c Release -r win-x64 --self-contained false (replace 'win -x64' with the platform you targetting) 3) And then to install it, use the sc.exe tool or Powershell: sc.exe create MyService binPath= "C:\path\to\your\published\executable.exe" 4) Next is to just start it...you can use sc.exe or Service Manager in Windows.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server