In my ASP.NET Core 2.0 project, I've been using IHostingEnvironment and IApplicationLifetime from Microsoft.Extensions.Hosting namespace.
In ASP.NET Core 2.1, these interfaces seem to be in both Microsoft.AspNetCore.Hosting and Microsoft.Extensions.Hosting namespaces. What is the difference and which namespace should I be using?
FYI, I am also using BackgroundService class which is now (Core 2.1) in Microsoft.Extensions.Hosting namespace.
A builder for hosted applications and services that helps manage configuration, logging, lifetime, and more. Contains the settings for constructing an HostApplicationBuilder. A program initialization utility. Context containing the common services on the IHost.
HttpContext.Items Property (Microsoft.AspNetCore.Http)Gets or sets a key/value collection that can be used to share data within the scope of this request.
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.
NET Core Hosting bundle is an installer for the . NET Core Runtime and the ASP.NET Core Module. The bundle allows ASP.NET Core apps to run with IIS.
Since ASP.NET Core 2.0 there are two hosting models:
Microsoft.AspNetCore.Hosting
namespace.Microsoft.Extensions.Hosting
namespace.There is some duplication of interfaces and other types between the two different models. For web applications you should generally use the types in the Microsoft.AspNetCore.Hosting
namespace except for the IHostedService
, BackgroundService
and other related types.
There is still work going on to move as much types and logic to the generic hosting model and improve the compatibility between the two. For example, IHostingEnvironment
and IApplicationLifetime
in the web hosting model might inherit from the same interfaces in the generic hosting model in a future release.
This article also explains it, and also explains what the situation is in .NET Core 3: https://andrewlock.net/ihostingenvironment-vs-ihost-environment-obsolete-types-in-net-core-3/
Basically in .NET Core 3, both interfaces are marked obsolete.
They are replaced like this:
The benefit: in .NET Core 3 IHostEnvironment is compatible in all apps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With