question

Mark-8838 avatar image
0 Votes"
Mark-8838 asked ZhiLv-MSFT answered

What Objects are Created in Asp.Net Core 5

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?

dotnet-aspnet-core-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You don't need answers from others, but can help yourself by simply checking what VS .NET profiler shows you, https://docs.microsoft.com/en-us/visualstudio/profiling/profiling-feature-tour?view=vs-2019#analyze-memory-usage

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @Mark-8838,

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.