Home > Net >  .Net 6 Console app: WebApplication.CreateBuilder vs Host.CreateDefaultBuilder
.Net 6 Console app: WebApplication.CreateBuilder vs Host.CreateDefaultBuilder

Time:12-15

I'm looking into .NET 6, and wanted to build a simple console application, with some dependency injection.

From what i can read, a lot has been done to make the startup (now just program) file, more readable. What does confuse me a bit is, that all improvements seems to have been made to WebApplication.CreateBuilderpart used in API projects, and not the Host.CreateDefaultBuilder. As mentioned in this blog

Microsofts own docs, also only seems to mention WebApplication.

To me it seems like WebApplication is only for web projects, like an API, and i can't find anything that confirms og debunks that.

Is it okay to use WebApplication in a console application, or should i rely on Host, and keep the stacked lambda expressions ?

CodePudding user response:

WebApplication.CreateBuilderpart() is only used for web/api applications like the name implies Host.CreateDefaultBuilder() is used to build a generic host (without web services, middleware etc) which you can use to build anything other than webhost.

See for example; https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host Which has not changed.

Its true that it feels a bit awkward to build console apps and/or backgroundservices at the moment.

  • Related