seagatewholesale.com

Streamlining Your .NET 6+ WebAPI: Transitioning from Startup.cs

Written on

Introduction

Prepare to say farewell to Startup.cs! With the launch of .NET 6, the responsibilities of Startup.cs have been integrated into Program.cs. If you're transitioning your ASP.NET WebAPI to version 6 or higher and looking to declutter your code, this guide is packed with useful insights. Just be careful not to toss that old Startup.cs file into the wrong bin!

Background

Before .NET 6, ASP.NET Core WebAPI projects generally contained both Program.cs and Startup.cs files in the root directory. However, with the introduction of .NET 6, the need for Startup.cs has been eliminated, as its functions have been absorbed into Program.cs. This means that any configurations or setups that were previously handled by Startup.cs will now take place in Program.cs.

Many developers are familiar with using Startup.cs for setting up services and middleware, and the name Startup.cs certainly reflects its purpose. But why did Microsoft decide to make this shift? Could Startup.cs potentially make a return in future .NET versions? Regardless of what lies ahead, it's crucial to stay flexible and open to adapting to new methodologies.

Approach to Refactoring Code

In this post, I created a functional sample ASP.NET Core Web API project. If you're feeling adventurous, you can explore the changes by checking out the links below:

This sample WebAPI project was generated using the OnionAPI template, which I recently upgraded to .NET 7. If you're looking to accelerate your development using a Clean Architecture approach, give this template a shot!

Source Code Overview

Program.cs and Startup.cs source code overview

Figure 1 displays the source code for Program.cs and Figure 2 shows the source code for Startup.cs prior to refactoring. The listings in these figures reflect the coding structure from .NET 5 or earlier. Note that the two functions, Main (line 9) and CreateHostBuilder (line 37) in Figure 1, are deprecated in .NET 6 and beyond, making Startup.cs obsolete.

Refactored Program.cs Source Code

Figure 3 illustrates the source code for Program.cs after refactoring.

Lessons Learned

  1. Migrating Code from Startup.cs to Program.cs

    The structure of Program.cs in .NET 6+ is divided into two primary sections: Registering Services and Running HTTP Pipeline (commonly referred to as Middleware). Refer to Figure 3, lines 16–42 for an example of Registering Services and lines 44–75 for Running HTTP Pipeline.

To visualize this process, think of it as preparing for an exciting road trip. You need to ensure your vehicle is ready (registering services) before you can hit the road (running the HTTP Pipeline). It might sound a bit convoluted, but keep the car metaphor in mind, and you'll grasp it easily!

  1. Code Reduction After Refactoring

    Initially, the combined lines of code in Program.cs and Startup.cs totaled 123. After careful refactoring, I've managed to condense this to a streamlined 87 lines of code in Program.cs. That's a reduction of 35 lines—definitely an improvement!

  2. Accessing DbContext in Program Class

    In the legacy Startup.cs file (Figure 2, line 58), the DbContext was called in the Configure section for Entity Framework Database initialization. Transitioning to Program.cs requires a different approach. For guidance, refer to lines 55–60 in Figure 3.

  3. Referencing Services and Middleware in Program.cs

    To reference third-party services or your custom projects in Program.cs, simply add the necessary namespaces at the top of the file. Check out example lines 55–60 in Figure 3 to see how it's done. If you want to keep things organized, consider utilizing the Global Usings feature.

References

Explaining the transition from Startup.cs to Program.cs is a considerable task for this short blog post. Fortunately, YouTube has you covered! Visit IAmTimCorey's video on "Cleaning Up The Program.cs File In .NET 6 Now That Startup.cs is Gone" for a deeper dive into the subject.

Summary

While it's possible that Startup.cs may return in future .NET versions, just like the Start button did in Windows, we must remember that software development is constantly evolving. Changes like consolidating Startup.cs into Program.cs aim to enhance processes and improve efficiency. Microsoft likely believes that this new structure will ultimately lead to a better experience for .NET developers.

Thank you for reading! If you found this helpful, please hit the "Follow" button below my profile. Your support inspires me to create even more valuable content for you. Wishing you a fantastic day ahead!

In this video, discover essential refactoring tips for transitioning from Startup.cs to Program.cs in .NET 7, enhancing your development workflow.

This video provides a comprehensive guide on cleaning up your Program.cs file in .NET 6, making the transition from Startup.cs seamless and efficient.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Spotlighting Hidden Gems: 10 Amazing Showcases on Medium

Discover outstanding articles from talented writers and join a vibrant community celebrating creative voices.

Understanding the Diverse Spectrum of Autism Misconceptions

Explore the variety of misconceptions surrounding autism, emphasizing individual differences and the complexity of the spectrum.

Unlocking New Heights in Content Creation with AI Tools

Discover how Chat GPT transformed my writing process and boosted productivity, blending technology with human creativity.

Crafting Your Personal Outline: A Writer's Essential Guide

Discover how a personal outline can enhance your writing process and ensure you prioritize yourself in your creative journey.

Embracing Self-Love: My Journey Beyond the Scale

A personal exploration of self-acceptance and strength through movement, leaving behind harmful weight obsession.

Transform Your Mindset: Steps to a Positive Perspective

Explore practical strategies to shift your mindset and embrace positivity through self-examination and actionable steps.

Understanding Django Project Structure: A Comprehensive Guide

Explore the essential files and folders in a Django project to understand its structure and functionality.

Resolving Git Merge Issues: A Practical Guide

Learn how to effectively fix a broken branch after a git merge or rebase with this step-by-step guide.