What Objects are Created in Asp.Net Core 5

Mark 1 Reputation point
2021-10-22T21:11:54.673+00:00

I'm a bit confused with how asp.net core is implemented, particularly .NET 5. I just wanted to know what objects are created, because I was reading the documentation and I just want to get some things straight. I know an instance of IHostBuilder is created and also an IWebHostBuilder as well and at the end it returns an IHost object. What exactly is the difference between IWebHostBuilder and IHostBuilder, are they the same object? And also what is the difference between the IHost and IWebHostEnvironment as well as the IApplicationBuilder in the Configure method?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,156 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2021-10-23T15:21:53.41+00:00

    You should learn the builder pattern first, then it will make more sense

    https://www.dofactory.com/net/builder-design-pattern

    0 comments No comments

  2. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2021-10-25T08:46:13.327+00:00

    Hi @Mark ,

    What exactly is the difference between IWebHostBuilder and IHostBuilder, are they the same object?

    They are not the same object.

    IWebHost Interface: Represents a configured web host. The web host is the general thing that hosts and runs your web application. It gets created when your application starts up, and then it will construct all the necessary pieces, like the Kestrel web server, the application middleware pipeline, and all the other bits, and connects them, so that your application is ready to serve your requests.

    IWebHostBuilder Interface: A builder for IWebHost. The host builder constructs the host and configures various of services. This is the generalization of the previous IWebHostBuilder but also basically does the same just for generic IHost. It configures the host before the application starts.

    You can check its properties and the methods and refer the application startup and hosting sections of the official documentation.

    And also what is the difference between the IHost and IWebHostEnvironment as well as the IApplicationBuilder in the Configure method?

    IHost: The host is the component that hosts and runs your application and its services. This is a generalization of the previous IWebHost but fullfills the same task: It starts configured hosted services and makes sure that your app is running and working.

    The IHostingEnvironment is an interface for .Net Core 2.0 and IWebHostEnvironment has replaced IHostingEnvironment from .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.

    1. WebRootPath – Path of the www folder.
    2. ContentRootPath – Path of the root folder which contains all the Application files.

    When you upload file to physical storage (such as the current project's www/file folder), you can use it.

    IApplicationBuilder: Defines a class that provides the mechanisms to configure an application's request pipeline. More detail information, see ASP.NET Core Middleware

    Besides, you can also check the ASP.NET Core fundamentals for understanding how to develop ASP.NET Core apps.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Dillion

    0 comments No comments