Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'IHostingEnvironment' is obsolete

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?

like image 732
kofifus Avatar asked Apr 09 '19 22:04

kofifus


People also ask

What can I use instead of IHostingEnvironment?

Net Core 2.0 and IWebHostEnvironment has replaced IHostingEnvironment in . Net Core 3.0. Both these interfaces need to be injected as dependency in the Controller and then later used throughout the Controller. Both these interfaces have two properties.

What is IHostEnvironment?

Extension Methods IsDevelopment(IHostEnvironment) Checks if the current host environment name is Development. This API should not be used in libraries, see remarks for details. IsEnvironment(IHostEnvironment, String) Compares the current host environment name against the specified value.

How do you use IHostingEnvironment?

ContentRootPath – Path of the root folder which contains all the Application files. You will need to import the following namespace. In the below example, the IHostingEnvironment is injected in the Controller and assigned to the private property Environment and later used to get the WebRootPath and ContentRootPath.

What is the role of IHostingEnvironment interface in asp net core?

What is the purpose of IHostingEnvironment interface in ASP.NET Core? Provides information about the web hosting environment an application is running in. In ASP.NET Core 3.0, IHostingEnvironment interfaces is marked obsolete. You can still use them, but you'll get warnings at build time.


2 Answers

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.

like image 109
Simply Ged Avatar answered Sep 18 '22 07:09

Simply Ged


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.

like image 33
Chris Pratt Avatar answered Sep 22 '22 07:09

Chris Pratt