ASP.NET Core 2.2 -> 3.0 upgrade. env.IsDevelopment() not found

asp.net Coreasp.net Core-3.0

asp.net Core Problem Overview


I upgraded an existing 2.2 project to 3.0. I copied the new code for Program/Startup from a new 3.0 project to my existing 2.2 project. It worked, but the IsDevelopment() below:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   if (env.IsDevelopment())
   {
      app.UseDeveloperExceptionPage();
   }
}

Results in this error: > 'IWebHostEnvironment' does not contain a definition for 'IsDevelopment' and the best extension method overload 'HostingEnvironmentExtensions.IsDevelopment(IHostingEnvironment)' requires a receiver of type 'IHostingEnvironment'

The same line did not caused a newly created 3.0 project. What do I need to modify/add to the project upgraded from 2.2?

asp.net Core Solutions


Solution 1 - asp.net Core

The new IHostEnvironment, IsDevelopment, IsProduction etc. extension methods are in the Microsoft.Extensions.Hosting namespace which may need to be added to your app.

Reference:

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#migrate-startupconfigure

https://github.com/aspnet/AspNetCore/issues/7749

Solution 2 - asp.net Core

As Rena says IsDevelopment has been moved to IHostEnvironment Interface in the Microsoft.Extensions.Hosting Namespace

I just had to add the

using Microsoft.Extensions.Hosting;

and then I could use IsDevelopment() as before.

Solution 3 - asp.net Core

int the extension:

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

public static class HostingEnvironmentExtensions
{
    public static IConfigurationRoot GetAppConfiguration(this IWebHostEnvironment env)
    {
        return AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName, env.IsDevelopment());
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDamn VegetablesView Question on Stackoverflow
Solution 1 - asp.net CoreRenaView Answer on Stackoverflow
Solution 2 - asp.net CoreGregView Answer on Stackoverflow
Solution 3 - asp.net CoreAlison HoracioView Answer on Stackoverflow