Namespace .AspNetCore.Hosting vs .Extensions.Hosting

Since ASP.NET Core 2.0 there are two hosting models:

  • The ASP.NET Core web hosting model we always had: this lives in the Microsoft.AspNetCore.Hosting namespace.
  • A new, generic hosting model to support the infrastructure and cross-cutting concerns in other applications such as console apps and Windows Services: this lives in the 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:

  • Microsoft.Extensions.Hosting.IHostingEnvironment (.NET Core 2.x)
    with Microsoft.Extensions.Hosting.IHostEnvironment (.NET Core 3.x)
  • Microsoft.AspNetCore.Hosting.IHostingEnvironment (.NET Core 2.x)
    with Microsoft.AspNetCore.Hosting.IWebHostEnvironment (.NET Core 3.x) which now inherits from IHostEnvironment !!

The benefit: in .NET Core 3 IHostEnvironment is compatible in all apps.

Tags:

Asp.Net Core