C
C#2mo ago
Immortal

✅ Difference between CreateApplicationBuilder and CreateDefaultBuilder?

Only difference I can see is that CreateApplicationBuilder returns a HostApplicationBuilder and CreateDefaultBuilder returns a HostBuilder?
5 Replies
Immortal
Immortal2mo ago
Like don't these code blocks do the same thing?
var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddHostedService<Bot>();

builder.Services.AddSingleton<DiscordClient>((serviceProvider) =>
{
var config = builder.Configuration;

var discordClient = new DiscordClient(new DiscordConfiguration
{
Token = config["DiscordBotToken"],
TokenType = TokenType.Bot,
AutoReconnect = true,
Intents = DiscordIntents.All
});

var slash = discordClient.UseSlashCommands(new SlashCommandsConfiguration
{
Services = serviceProvider
});

SlashRegistry.RegisterCommands(slash);

return discordClient;
});
var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddHostedService<Bot>();

builder.Services.AddSingleton<DiscordClient>((serviceProvider) =>
{
var config = builder.Configuration;

var discordClient = new DiscordClient(new DiscordConfiguration
{
Token = config["DiscordBotToken"],
TokenType = TokenType.Bot,
AutoReconnect = true,
Intents = DiscordIntents.All
});

var slash = discordClient.UseSlashCommands(new SlashCommandsConfiguration
{
Services = serviceProvider
});

SlashRegistry.RegisterCommands(slash);

return discordClient;
});
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
{
services.AddHostedService<Bot>();

services.AddSingleton<DiscordClient>((serviceProvider) =>
{
var config = serviceProvider.GetRequiredService<IConfiguration>();

var discordClient = new DiscordClient(new DiscordConfiguration
{
Token = config["DiscordBotToken"],
TokenType = TokenType.Bot,
AutoReconnect = true,
Intents = DiscordIntents.All
});

var slash = discordClient.UseSlashCommands(new SlashCommandsConfiguration
{
Services = serviceProvider
});

SlashRegistry.RegisterCommands(slash);

return discordClient;
});
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
{
services.AddHostedService<Bot>();

services.AddSingleton<DiscordClient>((serviceProvider) =>
{
var config = serviceProvider.GetRequiredService<IConfiguration>();

var discordClient = new DiscordClient(new DiscordConfiguration
{
Token = config["DiscordBotToken"],
TokenType = TokenType.Bot,
AutoReconnect = true,
Intents = DiscordIntents.All
});

var slash = discordClient.UseSlashCommands(new SlashCommandsConfiguration
{
Services = serviceProvider
});

SlashRegistry.RegisterCommands(slash);

return discordClient;
});
viceroypenguin
viceroypenguin2mo ago
CreateDefaultBuilder() literally just creates a host builder with an empty ServiceCollection. It also doesn't have any properties for anything on the host, so you have to configure everything via the .ConfigureXxx() methods. At this point, it's basically a legacy type. CreateApplicationBuilder creates a host builder that's used for top-level statements to start an application. There are properties from which to access various application elements (Services, Configuration, etc.), and some stuff is already pre-configured (using environment variables and appsettings.json for configuration; a default logger to the console; and which environment the app is running in; etc.
Immortal
Immortal2mo ago
I see, so is there any reason to use CreateDefaultBuilder() over CreateApplicationBuilder()?
viceroypenguin
viceroypenguin2mo ago
none, really
Immortal
Immortal2mo ago
Guessing that's why worker template uses CreateApplicationBuilder() now? since from what I've seen, in the past worker template used to use CreateDefaultBuilder() Thanks for the explanation