Aspnetcore.docs: Minor - "Host" missing in sample code

Created on 4 Dec 2019  Β·  6Comments  Β·  Source: dotnet/AspNetCore.Docs

For the sample code at the start of the "Create logs in the Program class" section, CreateDefaultBuilder(args) should be Host.CreateDefaultBuilder(args) -- the Host portion is missing.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P1 Source - Docs.ms doc-bug

Most helpful comment

BACK! :smile: ... that treadmill nearly πŸ’€ me! πŸƒπŸƒπŸƒπŸ˜…

It's best if we show the recommended approach that calls CreateHostBuilder. So, here's a more fleshed-out example of what I propose for that example ...

public static void Main(string[] args)
{
    var host = CreateHostBuilder(args).Build();

    var todoRepository = host.Services.GetRequiredService<ITodoRepository>();
    todoRepository.Add(new Core.Model.TodoItem() { Name = "Feed the dog" });
    todoRepository.Add(new Core.Model.TodoItem() { Name = "Walk the dog" });

    var logger = host.Services.GetRequiredService<ILogger<Program>>();
    logger.LogInformation("Seeded the database.");

    IMyService myService = host.Services.GetRequiredService<IMyService>();
    myService.WriteLog("Logged from MyService.");

    host.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

All 6 comments

Hello @MV10 ... Referring to the first example? It's ok ... it's calling CreateHostBuilder, which returns an IHostBuilder. It could add a little boilerplate, but that might be overkill.

... existing code for "public static void Main(string[] args)" ...

public static IHostBuilder CreateHostBuilder(string[] args)
{
    ...
}

It won't compile that way, Host. is missing:

image

It should be:

public static void Main(string[] args)
{
    var host = Host.CreateHostBuilder(args).Build();
...etc.

image

It’s not the complete example. The CreateHostBuilder method calls Host.CreateDefaultBuilder and returns an IHostBuilder. It can show the code I suggested. See the next example in this section. I’m at the πŸ‹ gym πŸ’ͺ on the treadmill ... hard to type a little more context for what I’m proposing.

BACK! :smile: ... that treadmill nearly πŸ’€ me! πŸƒπŸƒπŸƒπŸ˜…

It's best if we show the recommended approach that calls CreateHostBuilder. So, here's a more fleshed-out example of what I propose for that example ...

public static void Main(string[] args)
{
    var host = CreateHostBuilder(args).Build();

    var todoRepository = host.Services.GetRequiredService<ITodoRepository>();
    todoRepository.Add(new Core.Model.TodoItem() { Name = "Feed the dog" });
    todoRepository.Add(new Core.Model.TodoItem() { Name = "Walk the dog" });

    var logger = host.Services.GetRequiredService<ILogger<Program>>();
    logger.LogInformation("Seeded the database.");

    IMyService myService = host.Services.GetRequiredService<IMyService>();
    myService.WriteLog("Logged from MyService.");

    host.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Doh! Can't believe I overlooked that. (I'm not a fan of the CreateHostBuilder approach since it doesn't work well for larger, real-world apps, so I'd forgotten MS does that everywhere.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Raghumu picture Raghumu  Β·  3Comments

wgutierrezr picture wgutierrezr  Β·  3Comments

madelson picture madelson  Β·  3Comments

StevenTCramer picture StevenTCramer  Β·  3Comments

davisnw picture davisnw  Β·  3Comments