The name WebHost does not exists in current contex

2020-08-09 07:57发布

问题:

I'm migrating from ASP.NET Core 1.x to v2.0 with the help of following post on docs.microsoft: https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/

I'm almost done with all the changes mentioned in that post. But there is one error that is causing troubles.

Here is my Program.cs file:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;

namespace MeridiaCoreAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
           WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>()
            .ConfigureAppConfiguration((hostContext, config) =>
            {
            // delete all default configuration providers
            config.Sources.Clear();
                config.AddJsonFile("myconfig.json", optional: true);
            })
               .Build();
    }
}

And here is the error message:

Suppression State
Error   CS0103  The name 'WebHost' does not exist in the current context

Any solution, workaround or hint would be highly appreciated. Thanks.

回答1:

WebHost class resides Microsoft.AspNetCore assembly that comes with Microsoft.AspNetCore.All NuGet package. So to fix you problem install this NuGet package and add following using directive to your source file:

using Microsoft.AspNetCore;



回答2:

As CodeFuller's answer indicated the WebHost class is available in the assembly Microsoft.AspNetCore If you don't need everything, you can just get the package Microsoft.AspNetCore.