This blog is presented as a part of C# Advent 2023. Follow the link to check out the rest of the excellent C# and .NET content coming out as 2 blogs per day between December 1 - 25.
Many C# and .NET developers are grappling with aging ASP.NET projects still running on the .NET Framework. If that’s you, you’re not alone. Many of us have invested heavily in these applications, so the prospect of a full-scale upgrade can seem overwhelming. You might have your eye on moving to ASP.NET Core with its array of features and performance enhancements, but how do you get there? Do you have to do it all at once or is there a gradual way?
In this article, we’ll explore an incremental approach to migrating your legacy ASP.NET projects to ASP.NET Core. We’ll introduce you to a powerful tool called YARP (Yet Another Reverse Proxy), which can facilitate this process and make it more manageable, and a Visual Studio extension called the .NET Upgrade Assistant that will help you through the process.
The Challenge of Migration
The challenge with upgrading large ASP.NET applications lies in the fact that they’re often built on a .NET Framework, and transitioning them to ASP.NET Core involves significant effort. The substantial improvements in ASP.NET Core, while highly beneficial, also introduce breaking changes that make the upgrade process more complex than other types of projects.
The “big bang” approach of locking a team in a room until the entire application is upgraded is rarely a practical option, especially for larger projects. It’s not at all agile, more prone to errors, and can lead to frustration among team members.
Another viable approach is the evolutionary method, which involves refactoring the application in waves. This incremental process allows for adjustments, refactoring, and continuous improvement. However, coordinating this with production releases can be a challenge.
The Strangler Fig Approach
Enter the “strangler fig” pattern, inspired by a plant by the same name that germinates in the canopy of trees, grows vines down the tree, and wraps around its roots, eventually killing the tree and using it as a backbone to reach the sunshine.
This tactic used by nature, though violent, makes for a good analogy for gradual migrations of software projects. Using the strangler fig approach involves setting up a facade layer that brings together the old and new applications under one roof. The front-end interface remains stable, while the backend gradually transitions to ASP.NET Core. Eventually, the legacy application is entirely replaced, and the facade layer can be removed.
Meet YARP: Yet Another Reverse Proxy
YARP is a customizable reverse proxy built on top of ASP.NET Core and .NET. Developed by Microsoft, it’s tailored for .NET developers and seamlessly integrates with ASP.NET Core applications. Because it integrates with ASP.NET Core, YARP runs smoothly on Windows, Linux, MacOS, and in containers, and adding it to your project is as simple as installing a NuGet package.
Whie it can be used for many different purposes, YARP is perfect for implementing the strangle fig pattern to migrate a legacy ASP.NET application to ASP.NET Core. It allows you to gradually migrate your application while using the proxy to unite the two apps into one apparent app to your end-user. As you can see below, YARP becomes your facade layer but runs completely within your modern app.
Understanding Reverse Proxies
A reverse proxy, in essence, does the opposite of what a regular (forward) proxy does. Instead of forwarding requests from the local network out into the internet, a reverse proxy forwards requests from clients on the internet into one or more servers on your network (or your cloud provider’s network).
In the context of YARP being used for legacy migrations, this allows requests to be directed to either the legacy ASP.NET application or the new ASP.NET Core application based on what endpoints exist and your configuration settings.
A tool that can be very helpful when doing any migration in .NET is the .NET Upgrade Assistant. It streamlines the process of many different times of upgrade projects, including ASP.NET. For our purposes, it can help you automate the setup of your existing ASP.NET app, YARP, and a new modern ASP.NET Core project. It also helps you throughout your gradual code migration process by tracking your progress and helping you move controllers and other classes from your legacy ASP.NET project to the new ASP.NET Core project. While some manual adjustments may be needed, this tool saves you significant time and effort by streamlining and tracking the migration process for you.
- Install the .NET Upgrade Assistant Visual Studio extension. You can find more information on how to install it here.
- Open your ASP.NET project in Visual Studio.
- In the Solution Explorer window, right-click on the project you want to upgrade and select Upgrade.
- Select Side-by-side incremental project upgrade, which is the only upgrade option available for upgrading from ASP.NET to ASP.NET Core with YARP.
- For the upgrade target, select New project if you want it to create a new one for you, otherwise Existing project if you already have one, then click Next.
After following these steps, the .NET Upgrade Assistant will put a new ASP.NET Core project next to your existing ASP.NET legacy project and sets up YARP to route any endpoints that are implemented in the new project there, while all other calls are sent to the legacy ASP.NET application.
This mode lets you slowly upgrade your ASP.NET app piece-by-piece to the new ASP.NET Core app without even having to make configuration changes throughout the process. You can deploy these apps many times throughout the migration process, allowing you to follow an agile process for your migration.
At any time during the migration project, you can right-click on the legacy ASP.NET project in Solution Explorer to get a status report of your migration progress or to use tooling to help you automatically migrate classes and controllers to the ASP.NET Core project.
Migrating your legacy ASP.NET projects to ASP.NET Core is a crucial step toward harnessing the full potential of modern web development. With YARP and the incremental strangler fig approach, this transition becomes manageable, allowing you to leverage the benefits of ASP.NET Core without the need for a massive overhaul.
Don’t let the complexity of migration hold you back. Embrace the power of gradual ASP.NET migrations using the .NET Upgrade Assistant and YARP, unlocking a new era of possibilities for your applications.
If you need help with this process, contact Trailhead and we can guide you through your legacy migration project safely and smoothly.