The name WebHost does not exists in current context

Azaz ul Haq picture Azaz ul Haq · Nov 21, 2017 · Viewed 13.6k times · Source

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.

Answer

CodeFuller picture CodeFuller · Nov 21, 2017

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;