A Cleaner Startup for .NET 6’s Minimal Approach

A cleaner approach to application startup and configuration in .NET 6 after Microsoft both killed and kept the old startup file.

Stephen Walsh
ITNEXT
2 min readJun 29, 2022

--

.NET 6 Application Startup
Photo by Christian Paul Stobbe on Unsplash

After reading the .NET Migration Documentation in the past week, I have formulated an alternative to the suggested methods from Microsoft. To be honest, I created this approach last year and recently refined it, to make it ready to share with others.

The starting point was finding some posts about using the Startup.cs file in the new .NET 6 minimal hosting model. I know I'm not alone in finding Microsoft's format more annoying than anything. With Microsoft removing the Startup.cs file as the default, this lead me to assume they were proposing that Program.cs would be a heavy and long mess. In reading the posts and Microsoft's documentation I've come to realise that they have a suggested implementation. Problem is I think the implementation is a little messy.

Microsoft’s Solution

As I read through their documentation I found that…

Apps migrating to 6.0 don’t need to use the new minimal hosting model.

This was a surprise because I thought they’d killed the Startup.cs file and it’s a problem because I found that this eats away at their minimal model. The approach causes a few extra lines to be inserted into Program.cs to handle all the service and middleware configuration. It creates a new format Startup.cs file, that consists of two methods and an injected set of Configuration, easy enough and does the job.

Microsoft’s Documented Approach to Application Startup in .NET 6

Combined with the following Startup.cs class

Microsoft’s Documented Approach to Application Startup in .NET 6

Alternative Solution

The above code works just fine, it’s maybe a little clunky looking but it does the job. The alternative approach that I made is just a simple use of extension methods. Nothing world changing but I have found it to be a leaner and cleaner look. The extension methods pass in the WebApplicationBuilder and the WebApplication objects. WebApplication has access to the Environment configuration and both have access to the IConfiguraiton as well. This keeps the spirit of the old Startup.cs just like Microsoft's method but I've found it keeps the Program.cs clean and clear as well.

Alternative Approach to Application Startup in .NET 6

Combined with the following ProgramExtentions.csclass.

Alternative Approach to Application Startup in .NET 6

This method is not going to change your life but I have found value in it. A little pedantic, yeah probably but I enjoy the simplicity those extension methods offer. If they blow out in size, it’s possible to break them into separate files for each method. If any configuration gets even larger and more important, it’s possible to make ConfigureApplication and a ConfigureExceptionHandling methods. The possibilities are endless with that pattern.

Connect or Support?If you like this, or want to checkout my other work, please connect with me on LinkedIn, Twitter or GitHub, and consider supporting me at Buy Me a Coffee.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by Stephen Walsh

Hello! I’m a Husband, Father, Software Engineer, Learner & Leader. I live in Wisconsin, and spend my days trying to balance all of the above.

Responses (1)

What are your thoughts?