153

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?

3 Answers 3

255

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://learn.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

3
  • 36
    No need to install the Microsoft.Extensions.Hosting package, just add the using statement as mentioned in @Greg's post.
    – Luke
    Oct 13, 2019 at 11:36
  • so then we need both Microsoft.Extensions.Hosting and using Microsoft.AspNetCore.Hosting; at the same time... Wonder how that works? Class name from one package but its method from another?
    – Macindows
    Sep 9, 2020 at 8:50
  • Actually,the asp.net core 3.0 do not have such issue now.It has been fixed.
    – Rena
    Sep 9, 2020 at 8:54
140

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.

1
  • Thanks a million, this answer helped me to solve the exception. Was migrating asp.net core 23.0 to core 3.1. just add this library and all worked like sharp. Billiant! Nov 17, 2020 at 7:58
1

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());
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.