无法通过在控制台应用程序中使用自托管 AspNetCore MVC 作为 Windows 服务来获取动态 html 页面

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

问题:需要能够安装 运行 Asp.Net MVC 应用程序(returns SPA angular 应用程序)作为 Windows 服务

主题:我在控制台应用程序中使用 ASp.Net 核心 MVC,运行 它作为 Windows 服务,它工作得很好(所有请求都到达正确的位置)但是 return View(); returns 空白页面而不是右侧 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;
        }
    }
}

示例工程可以下载here

更新:

包括app.UseDeveloperExceptionPage()之后;进入 Configure 方法得到这样的错误列表

针对 .NET v4.6.1 创建一个新的 .NET Core Web 应用程序。请参阅下面的屏幕截图。

这样您将默认获得一个 exe Web 应用程序,它以 .NET v4.6.1 为目标。