【发布时间】:2018-08-26 10:22:14
【问题描述】:
我可以成功地自行托管我的 ASP.NET Core 应用程序,而不是托管在 IIS 中。
但现在我面临一个问题。
下面是我的.NET core项目,是一个控制台应用项目。
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:
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"));
}
}
}
一切正常,结果截图如下:
修改Startup类中的ConfigureServices方法如下后,问题出现了。
public void ConfigureServices(IServiceCollection services)
{
// Includes support for Razor Pages and controllers.
services.AddMvc();
}
在Program 类中调用BuildWebHost 方法时出现错误消息:
"The ConfigureServices method must either be parameterless or take only one parameter of type IServiceCollection."
任何帮助将不胜感激!
【问题讨论】:
-
清理并重建。你所拥有的应该可以工作。
-
分享.csproj的内容,无法重现你的问题,试试
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> <StartupObject>CoreMVC2_0.Program</StartupObject> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" /> </ItemGroup> </Project>之类的内容
标签: c# asp.net asp.net-core asp.net-core-mvc