Hello, Porting Assistant for .NET

Hello, Porting Assistant for .NET

This episode: Porting Assistant for .NET and modernizing .NET Framework applications. In this Hello, Cloud blog series, we're covering the basics of AWS cloud for newcomers who are .NET developers. If you love C# but are new to AWS, or to this particular tool, this should give you a jumpstart.

In this post we'll introduce Porting Assistant for .NET and use it to migrate a .NET Framework application to modern dotnet (formerly called .NET Core). We'll do this step-by-step, making no assumptions other than familiarity with C# and Visual Studio. We're using Visual Studio 2022 and .NET 6.

Porting Assistant : What is it, and why use It?

"To reach a port we must sail, sometimes with the wind, and sometimes against it. But we must not drift or lie at anchor." ―Oliver Wendell Holmes, Sr.

It is commonly estimated that 75% of enterprise appilcations are .NET based, and a great many of those are legacy applications on the .NET Framework. It's been more than 8 years since cross-platform .NET (originally called .NET Core) was announced in 2014, but you may still have legacy .NET framework applications running. There are 2 good reasons to port your legacy code to modern dotnet. First, the legacy .NET Framework is no longer advancing, while modern dotnet is alive and well, with new features and performance improvements every year. Second, you can reduce your costs on modern dotnet by running on Linux.

Those are good benefits, but how do you weigh them against the level of effort in porting? You might find yourself waist-deep in porting issues, such as dependencies on packages and APIs that aren't compatible with modern dotnet. How can you make porting easier? Happily, there is help available―and it comes at no charge.

Porting Assistant for .NET (PA) is a tool from AWS that helps you port your .NET Framework code to modern dotnet. AWS describes it as "an analysis tool that scans .NET Framework applications and generates a .NET Core compatibility assessment, helping you port your applications to Linux faster."

PA is an assistive tool. That means it will help you port, but some manual effort will likely remain. It reduces your level of effort but doesn't completely eliminate it.

Here's how it works. First PA scans your .NET Framework applications to discover APIs and NuGet packages that aren't compatible with modern dotnet. Next, a compatibility assessment report is generated, which suggests available replacements. Lastly, your code is ported, with packages and project references updated for you. PA can also visualize your application dependencies graphically.

One other thing to know about PA is that its dataset is open source, combining data from AWS and other public sources. The dataset that drives it is publicly available on GitHub.

Below is a walkthrough of Porting Assistant. That's followed by a short tutorial you can try yourself.

Walkthrough

Now, let's see what it's like to use PA with a non-trivial application. We'll run it against the AWS sample app GadgetsOnline. GadgetOnline is a .NET Framework MVC web application for online shopping that uses a SQL Server database. Here's what it looks like:

Opening the solution in Visual Studio, we see a .NET Framework 4.7.2 app with MVC controllers.

We launch Porting Assistant for .NET, a desktop tool, and assess the application. The assessment overview shows that 8 of 15 NuGet packages are incompatible and 43 out of 66 APIs are incompatible. We can review the analysis details on different tabs.

NuGet tab

On the NuGet tab, we see the specific packages and their compatibility status.

Project References tab

On the Project references tab, you can see a graph of projects referenced by other projects. In the case of GadgetsOnline, there's just one project.

In comparison, here's what the visualization looks like for a more complex application, nopCommerce:

APIs tab

On the API tab, APIs and compatibility statuses are listed. PA will offer suggested replacements for some of them. For example, replacing System.Web.Mvc with Microsoft.AspNetCore.Http. PA will also recommend open source solutions to compatibility problems, such as replacing Windows Community Foundation (WCF) with CoreWCF, an open source project that AWS and Microsoft contribute to.

Source files tab

The Source files tab gives you a view of source code, annotated with comments. Select a source code file to see exactly what changes PA intends to make.

The annotations tell you what code changes PA is planning when it is confident of a porting action.

When PA doesn't have a plan of action, it will call out the incompatible code, which you'll be responsible for porting. PA will recommend strategies for some of them.

Port

At this point, you now know what you're in for to port this application to modern dotnet, and what PA will do to assist you. You might decide to move forward with porting, or rewrite the app, or just leave it as is. If you decide to move forward, you click Port solution and PA ports your code.

Here's GadgetsOnline after PA has ported it. It's now a .NET 6 application. There are 28 errors the developer still has to address, but it would have been a much larger number without the assistance PA provided.

Our Hello, Porting Assistant Project

Now it's your turn. We will use a really simply example to give you the experience of using Porting Assistant for .NET from start to finish.

Step 1: Create a .NET Framework console app

In this step, you'll create a simple .NET Framework console app as our starting point. You can either follow the steps below, or just get the code from the source code GitHub repo.

  1. Launch Visual Studio or your favorite IDE, and create a new .NET Framework console application named csv_to_json.

  2. Open Program.cs in the code editor and replace it with the code below. This code reads a CSV file and displays the data as JSON, using code found at https://qawithexperts.com/article/c-sharp/convert-csv-to-json-in-c/465.

  3. Build the app.

  4. Close Visual Studio.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace csv_to_json
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Convert CSV to JSON
            // source: https://qawithexperts.com/article/c-sharp/convert-csv-to-json-in-c/465

            var csv = new List<string[]>();
            var csvFile = args[0];
            var lines = System.IO.File.ReadAllLines(csvFile); // csv file location

            // loop through all lines and add it in list as string
            foreach (string line in lines)
                csv.Add(line.Split(','));

            //split string to get first line, header line as JSON properties
            var properties = lines[0].Split(',');

            var listObjResult = new List<Dictionary<string, string>>();

            //loop all remaining lines, except header so starting it from 1
            // instead of 0
            for (int i = 1; i < lines.Length; i++)
            {
                var objResult = new Dictionary<string, string>();
                for (int j = 0; j < properties.Length; j++)
                    objResult.Add(properties[j], csv[i][j]);

                listObjResult.Add(objResult);
            }

            // convert dictionary into JSON
            var json = JsonConvert.SerializeObject(listObjResult);

            //print
            Console.WriteLine(json);
        }
    }
}

Step 2: Test the .NET Framework version

In this step, you'll test the original program to see it work.

  1. Use notepad or another text editor, create a test CSV file named orders.csv in the project folder with this content below.

  2. Open a command window and CD to the csv_to_json project folder.

  3. Run the command bin\debug\csv_to_json orders.csv

  4. You see the CSV data displayed as JSON.

CustomerId,Qty,SKU,Subtotal
1045,2,WIDGET-001,$18.98
1036,1,SPROCKET-311,$49.99

Now that we know what our program does, let's port it to modern dotnet.

Step 3: Port the solution with Porting Assistant

Now, we'll use PA to port the solution.

  1. Install Porting Assistant for .NET by visiting its product page and clicking Download Porting Assistant for .NET. Run the installer.

  2. Launch Porting Assistant for .NET. On the Settings page, clik Edit and set the target .NET version. We're using .NET 6 here.

3. On the Assessed solutions page, click Assess a new solution.

4. Click Choose file and select the csv_to_jsn solution file. Then click Assess.

5. Wait for a green message that the assessment has been completed.

6. Click the csv_to_json application name to see the asssesment detail. This simple app does not pose any porting obstacles.

7. Click Port solution. Choose the option to Modify source in place, and click Save. Then click Port.

8. Wait for green messages confirming the code has been ported and re-assessed.

9. Close PA.

Step 4: Review and Build the Ported Solution

  1. In Visual Studio, again open the csv_to_json solution.

  2. Review the solution. Note that is now a .NET 6 solution, and has modern dotnet structure including a Startup.cs file.

  3. Build the solution, and note build errors. We must add the package NewtonSoft.json.

  4. In Solution Explorer, right-click the project, select Manage NuGet Packages, and find and install package Newtonsoft.json.

  1. Build the project, which should now build without errors.

  2. Close Visual Studio.

Step 5: Test the Ported Solution

In this step, you'll test your ported modern dotnet solution.

  1. Return to the command window from Step 2.

  2. Run the program with this command dotnet run orders.csv

  3. The program runs, now on modern dotnet, with identical output as its original .NET Framework edition.

Congratulations, you successfuly ported a .NET Framework app to modern dotnet with Porting Assistant for .NET.

Where To Go From Here

Porting Assistant for .NET assists you in porting legacy .NET Framework applications to modern dotnet. It won't do the entire job for you, but it does the heavy lifting and reduces the amount of manual effort.

You might be on the fence about whether porting is worthwhile. Porting Assistant lets you know what the issues are, and helps with many of them. It's like assisted driving.

To go further, read and watch the resources below. Also take a look at the .NET Toolkit for Refactoring, which is a newer experience for our .NET modernization tools that can be used from Visual Studio. You can find more .NET modernization tools at the .NET on AWS developer center website, aws.amazon.com/dotnet.

Further Reading

AWS documentation

Porting Assistant for .NET

Infographics

Porting Assistant for .NET

Videos

AWS for the .NET Developer - Porting Assisstant for .NET by Isaac Levin

Blogs

Modernizing ASP .NET Web Forms applications to Blazor using Porting Assistant for .NET by Carlos Santos

GitHub

porting-assistant-dotnet-datastore