pmazaleu pmazaleu - 1 year ago 279
ASP.NET (C#) Question

Can't get dynamic html page by using Self hosted AspNetCore MVC inside console app as Windows Service

Problem: Need to be able to install and run Asp.Net MVC app (which returns an SPA angular app) as Windows Service

Subject: I'am using ASp.Net Core MVC inside Console app and run it as Windows service and it works perfectly fine (all requests are getting to the right place) but

return View();
returns empty page instead of the right html.

public class Startup
public IContainer ApplicationContainer { get; private set; }
public IConfigurationRoot Configuration { get; private set; }

public Startup(IHostingEnvironment env)
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)

if (env.IsDevelopment())
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
Configuration = builder.Build();


// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
// Add framework services.


services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

var builder = new ContainerBuilder();

this.ApplicationContainer = builder.Build();
return new AutofacServiceProvider(this.ApplicationContainer);

// This method gets called at the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

var listener = app.ServerFeatures.Get<WebListener>();
if (listener != null)
listener.AuthenticationManager.AuthenticationSchemes =

app.UseCors(builder => builder.AllowAnyOrigin());

app.UseMvc(routes =>
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
name: "defaultApi",
template: "api/{controller}/{action}/{id?}");

public class Program
public static string ServiceName = "WebApp";

static void Main(string[] args)
var exePath = Process.GetCurrentProcess().MainModule.FileName;
//sure it's not necessary! It was just a try
directoryPath = directoryPath.Replace("\\bin", "");
var directoryPath = Path.GetDirectoryName(exePath);
var httpAddress = "https://localhost";
var httpPort = "10778";
var host = new WebHostBuilder()
.UseWebRoot(directoryPath + "\\wwwroot") //also just a try to navigate it right

if (Debugger.IsAttached || args.Contains("--debug"))
catch (Exception ex)
var exMessage = ex.Message;

The sample project can be downloaded here


After including app.UseDeveloperExceptionPage(); into Configure method get such list of the errors

enter image description here

Answer Source

Create a new .NET Core Web Application targeting .NET v4.6.1. See screenshot below.

enter image description here

This way you will get an exe web application by default, and it targets .NET v4.6.1.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download