【问题标题】:How to self host ASP.NET Core Web App in Windows?如何在 Windows 中自托管 ASP.NET Core Web App?
【发布时间】: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的内容,无法重现你的问题,试试&lt;Project Sdk="Microsoft.NET.Sdk"&gt; &lt;PropertyGroup&gt; &lt;OutputType&gt;Exe&lt;/OutputType&gt; &lt;TargetFramework&gt;netcoreapp2.0&lt;/TargetFramework&gt; &lt;StartupObject&gt;CoreMVC2_0.Program&lt;/StartupObject&gt; &lt;/PropertyGroup&gt; &lt;ItemGroup&gt; &lt;PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" /&gt; &lt;/ItemGroup&gt; &lt;/Project&gt;之类的内容

标签: c# asp.net asp.net-core asp.net-core-mvc


【解决方案1】:

尝试从以下位置更改签名:

public void ConfigureServices(IServiceCollection services)

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    return services.BuildServiceProvider();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多