Share this post
  

Upgrading .NET 5 -> 6 using Minimal APIs, MySQL and GitHub Codespaces

PR #198 merged! Upgrade nginx-aspnet-mysql to net6.0 🚀 Here are the steps I took from moving from .NET 5 API endpoints to Minimal APIs using GitHub Codespaces

In this post I share some of the steps I took from submitting a PR for docker/awesome-compose repo in GitHub by upgrading code from .NET 5 -> 6, all from the confort of GitHub Codespaces, containers and the .NET tooling!

GitHub Link: PR #198 on docker/awesome-compose

Upgrading from .NET 5 to .NET 6 using Minimal APIs, MySQL and GitHub Codespaces

I enjoy working my code with GitHub codespaces. I use the product for blogging and developing my website, general-purpose repositories and most of my .NET samples under my GitHub profile. Some time ago, I decided to contribute to the awesome-compose repo by adding some .NET code samples which are available here.

The awesome-compose repo is big since it contains a lot of great samples in other languages so definitely I recommend to take a look when learning about Docker-Compose. For scope of this post we will focus on the /nginx-aspnet-mysql folder sapmle which I introduced some time ago by using .NET 5 with basic API endpoints.

In a nutshell, we will run Docker-compose in the cloud (no need to install Docker-Compose locally, but we can do it if wanted to expand later) with the help of Codespaces in GitHub, develop with .NET 6 and integrate it with a NGINX proxy and MySQL database with classic ADO.NET styled calls. All from the confort our our browser and VS Code online!

Set up a GitHub Codespaces for developing on Awesome-Compose repository

You can start a brand new Codespace right away from GitHub Codespaces website. Specifically for .NET 6, you can also build your own codespace with dependencies you need and use .NET 5/6/X at the same time. For this sample, we can open docker/awesome-compose directly in Codespaces and it will launch with default codespace image (Docker based).

In order to add support for .NET + Docker-Compose awesomeness, we can add some container sugar by just copy-paste the contents of the .devcontainer folder, which is actually another sample that I made in the past for targeting .NET 6.

git clone https://github.com/stvansolano/dotnet6-minimal-api.git

By doing so, Codespaces may ask you to rebuild your codespace and after it, we can start changes with our Program.cs, .csproj and Dockerfile files.

For this sample, I re-used files from the stvansolano/dotnet6-minimal-api which is based .NET 6.

In case you want to give it a try, you can list the installed SDKs versions of .NET from the Codespace with VS Code Command Prompt:

dotnet --list-sdks

Make sure .NET 6 is listed there and you would be ready to build your code.

For creating a new ASP.NET web project:

dotnet new web --name aspnetapp

Running Docker-Compose inside Codespaces

Once we have our .devcontainer files from previous section, we ca use the command line (CLI) in VS Code, which is supported on the browser by GitHub Codespaces. Finally, make sure to run docker-compose command under the /nginx-aspnet-mysql folder

docker-compose up -d

And that’s it! The code contains a basic sample that contains:

  • Nginx based proxy
  • ASP.NET Minimal Web API
  • Access a MySQL database from raw ADO.NET calls using MySqlConnector NuGet package

A recap of changes: Moving from .NET 5 to v6

If you are migrating from .NET 5 to 6, here is a quick summary of changes to be aware of my PR #198 in awesome-docker repo:

  1. You need to change the TargetFramework to net6.0 in .csproj file
  2. You can start to use the implicit usings in the .csproj by adding <ImplicitUsings>enable</ImplicitUsings> tag
  3. Move your endpoints up as part of app.MapGet calls in Program.cs file
  4. Refactor and clean-up the Program.cs file
  5. Build and fix compilation issues (a few in our sample)

Also, you may update references if needed, in this case MySqlConnector to latest version available (in this case, MySQL is supported in .NET 6!)

dotnet add package MySqlConnector --version 2.1.2

Happy coding!

Share this post
  


@stvansolano
More about me