.net core 3.1 Web API hosting as Windows Service not showing pages

Viewed 1358

I am trying to host a windows service 3.1 as windows service. However I keep getting the page as "This site can’t be reached"

If I run the application deployed in IIS everything works perfectly.

The .net Core has angular 8 client app as well.

Program.CS

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

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        }).ConfigureWebHost(config =>
        {
            config.UseUrls("http://*:9095/");
        }).UseWindowsService();

Startup.CS

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    // In production, the Angular files will be served from this directory
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/dist";
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseStaticFiles();
    if (!env.IsDevelopment())
    {
        app.UseSpaStaticFiles();
    }

    app.UseRouting();

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

    app.UseSpa(spa =>
    {
        
        spa.Options.SourcePath = "ClientApp";

        if (env.IsDevelopment())
        {
            spa.UseAngularCliServer(npmScript: "start");
        }
    });
}

And I was able to publish the project and create a windows service and start it.

enter image description here

Command used to create the Service:

**sc create MyWinService binPath="D:\MyWinService\bin\Release\netcoreapp3.1\win-x64\MyWinService .exe"

[SC] CreateService SUCCESS**

And Started the service from msc.service

After that accessing the URL : http://localhost:9095/ gives me no result

enter image description here

2 Answers

You are installing the wrong EXE file

sc create MyWinService binPath="D:\MyWinService\bin\Release\netcoreapp3.1\win-x64\MyWinService.exe

It should be

sc create MyWinService binPath="D:\MyWinService\bin\Release\netcoreapp3.1\publish\MyWinService.exe

D:\MyWinService\bin\Release\netcoreapp3.1\win-x64 does not contain the ClientApp folder

Add a rule in the Firewall to allow this service to be accessed using the given port.

Related