Search code examples
c#asp.netangularservice

How to run an asp net 6.0 C# program (with angular) as a service? (Windows 10)


So I've found lots of tutorials online for running C# programs as services, but I've been unable to find one that is specifically oriented for a asp net framework for a web gui and backend server component. In my case, the frontend is in angular although this probably doesn't matter. My program has a .exe compiled from asp net C# that, when run, starts the backend server, which waits for calls from the frontend web gui on ports 5000 and 5001. However, I am unable to convert this .exe into a service (via powershell commands such as those in sc.exe) that works. I can create the service, but I can never start it. I believe I am missing something in my program.cs regarding services:

IHost host = Host.CreateDefaultBuilder(args)
    .UseWindowsService(options =>
    {
        options.ServiceName = "DebugGui";
    })
    .Build();

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();

var app = builder.Build();

if (!app.Environment.IsDevelopment()) {
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.Urls.Add("http://0.0.0.0:5000");
app.Urls.Add("http://0.0.0.0:5001");

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html"); ;

app.Run();

As you can see, my host object, which I brought in from one of the service tutorials, isn't being used because I'm not sure HOW to use it. Additionally, I feel like doing app.Run() isn't the correct approach as that forces a command line interface to pop up and run the program through there, whereas a service wouldn't have such a thing (at least not visible to the user).

Tutorials I have already looked at/tried; they were helpful but were not specific enough for my case as they don't incorporate the web portion (var builder = WebApplication.CreateBuilder(args);); I'm basically trying to figure out how to add the web portion. https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-7.0&tabs=visual-studio https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-7.0&tabs=visual-studio (This one actually DOES have the web portion in it, but when I followed the tutorial, I got an error when trying to start the service saying it failed to start in a timely fashion. I went down a rabbit hole of researching this but found no solutions).

Any help is much appreciated.


Solution

  • So I know it's weird to answer your own question but I found the solution and wanted to write it down in case anyone else has this issue and finds this post.

    Turns out the code I had was already mostly correct, and the issue I was having was something else (still not sure what it is though).

    using Microsoft.Extensions.Hosting.WindowsServices;
    
    var options = new WebApplicationOptions
    {
        Args = args,
        ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
    };
    
    var builder = WebApplication.CreateBuilder(options);
    
    builder.Host.UseWindowsService(options => { options.ServiceName = "DebugGui"; });
    
    builder.Services.AddControllersWithViews();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    
    app.Urls.Add("http://0.0.0.0:5000");
    app.Urls.Add("http://0.0.0.0:5001");
    
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller}/{action=Index}/{id?}");
    
    app.MapFallbackToFile("index.html"); ;
    
    app.Run();
    

    Another thing I had to do was recreate the asp net 6.0 template project in visual studio 2022 instead of 2019. This seems to have fixed the issue with the service starting and then stopping instantly or timing out. I still don't know all the specifics though of why things weren't originally working from a non code standpoint (I was having lots of issues with the service itself), but I just wanted to post what worked for me.