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

时间:2016-10-03 08:44:08

标签: asp.net asp.net-mvc asp.net-mvc-5 asp.net-core self-hosting

问题:需要能够安装和运行Asp.Net MVC应用程序(它返回一个SPA角应用程序)作为Windows服务

主题:我在控制台应用程序中使用ASp.Net Core MVC并将其作为Windows服务运行,它运行得很好(所有请求都到达正确的位置)但return View();返回空页而不是对了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方法得到这样的错误列表

enter image description here

1 个答案:

答案 0 :(得分:0)

创建面向.NET v4.6.1的新.NET Core Web应用程序。见下面的截图。

enter image description here

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