92

I updated my ASP.NET Core project to .NET Core v3.0.0-preview3, and I now get:

Startup.cs(75,50,75,69): warning CS0618: 'IHostingEnvironment' is obsolete: 'This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment.'

The code is:

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
  if (env.IsDevelopment()) {
    …
  }
}

What is the correct way to do this now? Are there any documentation or examples to demonstrate that?

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
kofifus
  • 17,260
  • 17
  • 99
  • 173
  • 7
    It seems `IHostingEnvironment` has been replaced by `IHostEnvironment` (and a few others). See [this link](https://github.com/aspnet/AspNetCore/issues/7749) on GitHub – Simply Ged Apr 09 '19 at 22:08
  • 1
    great thanks, all I had to do was to replace `IHostingEnvironment` with `IHostEnvironment` and add `using Microsoft.Extensions.Hosting;` .. if you want to post an answer I'll accept it – kofifus Apr 09 '19 at 22:13
  • Some places i see method `public void Configure(IApplicationBuilder app)` and in some projects I start, the default is `public void Configure(IApplicationBuilder app, IWebHostEnvironment env)`. Whats the difference? – Ted Sep 07 '19 at 07:51
  • 1
    Well now it's right there in the warning, isn't it: `"The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment."` – jbyrd Nov 22 '19 at 20:58

5 Answers5

114

It seems IHostingEnvironment has been replaced by IHostEnvironment (and a few others). You should be able to change the interface type in your code and everything will work as it used to :-)

You can find more information about the changes at this link on GitHub https://github.com/aspnet/AspNetCore/issues/7749

EDIT There is also an additional interface IWebHostEnvironment that can be used in ASP.NET Core applications. This is available in the Microsoft.AspNetCore.Hosting namespace.

Simply Ged
  • 8,250
  • 11
  • 32
  • 40
  • 1
    a quick typo in above answer, IHostingEnvironment is replaced by IWebHostEnvironment. and a new namespace needs to be used "using Microsoft.Extensions.Hosting;". – Abhimanyu Oct 10 '19 at 18:20
  • 4
    What is the difference between `IHostEnvironment` and `IWebHostEnviornment` asked myself: `Use IHostEnvironment where possible, and use IWebHostEnvironment when you need access to the WebRootPath or WebRootFileProvider properties.` https://andrewlock.net/ihostingenvironment-vs-ihost-environment-obsolete-types-in-net-core-3/ – Riscie Nov 26 '19 at 10:54
  • They could have made this a lot simpler! – ATL_DEV Jun 03 '23 at 01:23
82

When Microsoft.Extensions.Hosting was introduced in 2.1 some types like IHostingEnvironment and IApplicationLifetime were copied from Microsoft.AspNetCore.Hosting. Some 3.0 changes cause apps to include both the Microsoft.Extensions.Hosting and Microsoft.AspNetCore.Hosting namespaces. Any use of those duplicate types causes an "ambiguous reference" compiler error when both namespaces are referenced.

This error has been addressed in 3.0.0-preview3 by marking the following types obsolete and replacing them with new types. There have not been any behavioral changes made for the new types, only naming.

Obsolete types (warning):

Microsoft.Extensions.Hosting.IHostingEnvironment
Microsoft.AspNetCore.Hosting.IHostingEnvironment
Microsoft.Extensions.Hosting.IApplicationLifetime
Microsoft.AspNetCore.Hosting.IApplicationLifetime
Microsoft.Extensions.Hosting.EnvironmentName
Microsoft.AspNetCore.Hosting.EnvironmentName

New types:

Microsoft.Extensions.Hosting.IHostEnvironment
Microsoft.AspNetCore.Hosting.IWebHostEnvironment : IHostEnvironment
Microsoft.Extensions.Hosting.IHostApplicationLifetime
Microsoft.Extensions.Hosting.Environments

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

For 3.0 both the old and new types will be available from HostBulder's and WebHostBuilder's dependency injection containers. The old types will be removed in 4.0.

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

Long and short, you're looking for IWebHostEnvironment now. You'll likely need to add using for Microsoft.Extensions.Hosting as well.

Community
  • 1
  • 1
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • 4
    FYI, Microsoft has pointed out that .NET CORE 4.0 will be skipped in order to avoid confusion with .NET Framework 4.0. Instead, the next version after .NET Core 3.x is .NET 5.0. https://github.com/dotnet/core/blob/master/roadmap.md – J Weezy Feb 20 '20 at 03:31
  • That was clipped from the Microsoft support document. At the time, they hadn't announced .NET 5 yet. – Chris Pratt Feb 20 '20 at 12:42
3

From ASP.NET Core 5, use:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    if (Environments.Development.Equals(env.EnvironmentName))
    {
        ...
  • How is this different from the accepted answer or other highly upvoted answers? It seems like the same guidance but with less explanation. – Jeremy Caney Sep 16 '21 at 00:12
  • 2
    @JeremyCaney Yes, this would certainly be a better answer if some explanation was included, and code-only answers are necessarily incomplete answers. But the OP provided some code, and then explicitly asked: "_What is the correct way to do this now?_". While other answers and comments provided discussion and detail of ASP.NET changes, this is the only answer which directly responds to the question that was asked, by posting the revised code. None of the other answers or comments provided a revised implementation of `Configure()`. – skomisa Sep 16 '21 at 03:22
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 16 '21 at 03:23
2

For env.IsDevelopment(), I used env.EnvironmentName.Equals("Development")

leocrimson
  • 702
  • 1
  • 11
  • 25
  • 3
    It's much better to use `IHostEnvironment` and then you get direct access to the `IsDevelopment()` extension method. – db2 Aug 09 '21 at 17:18
1

The type IHostingEnvironment is obsolete for ASP.NET Core 3.0+. And according to Microsoft:

The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.ihostingenvironment?view=aspnetcore-7.0

IHostingEnvironment is obsolete for following versions as of today: enter image description here

Fayyaz Naqvi
  • 695
  • 7
  • 19