Share via

How does ConfigureWebHostDefaults work

abbas izadipour 210 Reputation points
2023-06-24T08:41:29.9266667+00:00

In the following code snippet, I am having difficulty understanding where webBuilder came from. Assuming ConfigureWebHostDefaults is a method inside the parentheses, it should be a parameter being passed to the method. Within the parentheses, there is a function whose purpose I can understand. I also see that webBuilder is an IWebHostBuilder, but shouldn't we declare that variable somewhere before using it?

Here is the code snippet:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
              webBuilder.UseStartup<Startup>();
            });
Developer technologies | .NET | Other
Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author

Bruce (SqlWork.com) 84,061 Reputation points
2023-06-24T15:20:58.12+00:00

This is basic c# 101.

A common pattern is passing a callback (delegate) to method. The method defines the the supported delegate signature (parameters and return type). this pattern is so common, the Action<> and Func<> generics are defined. See:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.generichostbuilderextensions.configurewebhostdefaults?view=aspnetcore-7.0

In your sample code a lambda is used as the delegate.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.