Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace .AspNetCore.Hosting vs .Extensions.Hosting

Tags:

asp.net-core

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.

like image 382
Sandy Avatar asked Jun 13 '18 23:06

Sandy


People also ask

What is Microsoft Extensions hosting used for?

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.

What is the use of Microsoft AspNetCore HTTP?

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.

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.

What is .NET core hosting?

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.


2 Answers

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.

like image 113
Henk Mollema Avatar answered Nov 14 '22 02:11

Henk Mollema


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.

like image 37
sdec Avatar answered Nov 14 '22 02:11

sdec