A Cleaner Startup for .NET 6's Minimal Approach
Tags: dotnet, csharp, programming, tutorial
Published: 2022-06-29 07:12:25

After reading the [.NET Migration Documentation](https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-6.0&tabs=visual-studio) 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.

{% embed https://gist.github.com/stphnwlsh/4b302d1eb7b55a0e7e2881bf9e8c77b3 %}

Combined with the following `Startup.cs` class

{% embed https://gist.github.com/stphnwlsh/324ae9028048bdfd3bb0d7cf31dbcfd1 %}

---

## 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.

{% embed https://gist.github.com/stphnwlsh/f44c8898de4eafc877b105b86362e5b0 %}

Combined with the following ProgramExtensions.cs class

{% embed https://gist.github.com/stphnwlsh/062f68efc405e90cdca25bb3d27f1c5f %}

---

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.

---

## Support

If you like this, or want to checkout my other work, please connect with me on [LinkedIn](https://www.linkedin.com/in/stphnwlsh), [Twitter](https://twitter.com/stphnwlsh) or [GitHub](https://github.com/stphnwlsh), and consider supporting me at [Buy Me a Coffee]