pmazaleu pmazaleu - 1 month ago 196
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()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();

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.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc().AddWebApiConventions();

services.AddCors();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

var builder = new ContainerBuilder();
builder.Populate(services);

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)
{
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();

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

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

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

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

static void Main(string[] args)
{
try
{
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()
.UseWebListener()
.UseWebRoot(directoryPath + "\\wwwroot") //also just a try to navigate it right
.UseContentRoot(directoryPath)
.UseUrls($"{httpAddress}:{httpPort}")
.UseStartup<Startup>()
.Build();

if (Debugger.IsAttached || args.Contains("--debug"))
{
host.Run();
}
else
{
host.RunAsService();
}
}
catch (Exception ex)
{
var exMessage = ex.Message;
}
}
}


The sample project can be downloaded here

UPDATE:

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

enter image description here

Answer

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.

Comments