Host.CreateDefaultBuilder vs Host.CreateApplicationBuilder in .NET Platform Extension 7
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
The .NET Platform Extension 7 introduces new capabilities and refinements, including changes to application hosting scenarios. Two prominent methods for creating hosts in .NET are `Host.CreateDefaultBuilder` and `Host.CreateApplicationBuilder`. Understanding these methods, their use-cases, and differences can help in creating efficient and scalable .NET applications. This article delves into both methods and provides detailed insights augmented by examples.
Overview of Hosting in .NET
In .NET, the `Host` class is pivotal for setting up the common functionalities required by most applications such as dependency injection, configuration, logging, etc. The `Host` serves as an abstraction layer that facilitates these services in an application through a centralized mechanism.
Use of `Host.CreateDefaultBuilder`
`Host.CreateDefaultBuilder` is a staple for setting up console applications or services in .NET. It initializes a host with pre-configured defaults, including configuration from `appsettings.json` files, environment variables, the `IConfiguration` interface, and logging from built-in providers.
Code Example
- Configuration: Automatically loads configuration from various sources like environment variables, command-line arguments, and `appsettings.json`.
- Dependency Injection: Comes with built-in support to facilitate DI through `Microsoft.Extensions.DependencyInjection`.
- Logging: Sets up predefined logging providers, allowing easy access to comprehensive logging capabilities.
- Optimized for Web: Designed with minimal APIs and web hosting in mind, streamlining setup for ASP.NET applications.
- Customizability: Offers a more configurable entry point compared to its predecessor, focusing on reducing default services to optimize application startup.
- Performance: By minimizing predefined services, applications experience quicker startup times.
- Flexibility: `Host.CreateApplicationBuilder` caters to developers looking for a finer-grain control over their app's startup configuration and dependencies.
- Simplicity: Conversely, `Host.CreateDefaultBuilder` offers simplicity with a ready-to-use framework, providing a more predictable environment with conventional defaults.
- Evaluate Requirements: Assess if the current application's needs warrant the shift to `Host.CreateApplicationBuilder`. Minimalist web applications may benefit from the newer builder.
- Refactor Carefully: If migrating, ensure thorough testing and validation as configuration sources and DI registrations may need adjustments.
Related reading
- How are .NET 4 GUIDs generated?
- How are strings passed in .NET?
- How async works in ASP.NET web applications?
- How big is an object reference in .NET?
- How can a windows service programmatically restart itself?
- How can a Word document be created in C?
- How can I add a hint or tooltip to a label in C Winforms?
- How can I add a hint text to WPF textbox?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.