问题:需要能够安装和运行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方法得到这样的错误列表