[英]How to self host ASP.NET Core Web App in Windows?
I can self host my ASP.NET Core application successfully instead of hosting in IIS. 我可以成功地自行托管ASP.NET Core应用程序,而不是在IIS中托管。
But now i am facing one problem. 但是现在我面临一个问题。
Below is my .NET core project, it is a console application project. 下面是我的.NET核心项目,它是一个控制台应用程序项目。
Program.cs : Program.cs:
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System;
namespace ConsoleAppCoreWeb
{
class Program
{
static void Main(string[] args)
{
BuildWebHost(args).Run();
Console.WriteLine("Please enter any key to exit");
Console.ReadLine();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args).UseUrls("http://localhost:5001")
.UseStartup<Startup>()
.Build();
}
}
Startup.cs: Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleAppCoreWeb
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices()
{
// Includes support for Razor Pages and controllers.
//services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.Run(context => context.Response.WriteAsync("hello world"));
}
}
}
All worked fine and screenshots of result as below: 一切正常,结果截图如下:
After i modified the ConfigureServices
method in Startup
class as below, the problem appeared. 在按如下所示修改
Startup
类中的ConfigureServices
方法后,出现了问题。
public void ConfigureServices(IServiceCollection services)
{
// Includes support for Razor Pages and controllers.
services.AddMvc();
}
Error Message when invoke BuildWebHost
method in Program
class : 在
Program
类中调用BuildWebHost
方法时出现错误消息:
"The ConfigureServices method must either be parameterless or take only one parameter of type IServiceCollection."
Any help would be appreciate! 任何帮助将不胜感激!
Try change signature from: 尝试从以下位置更改签名:
public void ConfigureServices(IServiceCollection services)
to 至
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
return services.BuildServiceProvider();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.