TransWikia.com

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

Stack Overflow Asked by TBA on November 27, 2021

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:MyWinServicebinReleasenetcoreapp3.1win-x64MyWinService .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

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

Answered by R. Hoek on November 27, 2021

You are installing the wrong EXE file

sc create MyWinService binPath="D:MyWinServicebinReleasenetcoreapp3.1win-x64MyWinService.exe

It should be

sc create MyWinService binPath="D:MyWinServicebinReleasenetcoreapp3.1publishMyWinService.exe

D:MyWinServicebinReleasenetcoreapp3.1win-x64 does not contain the ClientApp folder

Answered by GRFRM on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP