2

Upgrading to .Net 6

 3 years ago
source link: https://dzone.com/articles/upgrading-to-net-6
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Upgrading to .Net 6

In this article I walk you through the experience I had with upgrading from .Net 5 to .Net version 6, how I did it, in 35 projects simultaneously.

Join the DZone community and get the full member experience.

Join For Free

.Net 6 SDK was just released by Microsoft, and apparently it's a really kick ass upgrade. It significantly optimises Stream, reduces memory footprint, and contains tons of new cool features. Probably the most important code improvement change for me personally, is the mutable DOM classes for System.Text.Json, which over time will allow me to completely replace Newtonsoft.JSON with the builtin libraries, reducing dependencies in my core. Dependencies is a big thing for me in Magic, which I'm trying to reduce to a minimum. In fact, dependency reduction is arguably my single most important axiom for improving code quality and modularity, something I guess the fact that Magic contains 30+ projects is a testimonial towards.

My experience with updating Magic to use version 6 was almost 100% painless. Basically, I did a search and replace through my .csproj file replacing "net5.0" with "net6.0", and everything (almost) immediately worked. 1,000+ unit tests are green, 125+ integration tests are green, and while manually quality assuring features in Magic after updating, I was not able to find any breaking changes at all for me personally. Of course, I am trying as much as possible to use .Net Standard as much as I can in most of these projects, but still there were 20+ .csproj files that were updated during this process.

Microsoft did a great job with .Net 6. Several (fake) compiler warnings simply disappeared, such as the Analyzer DLL loaded twice thing, which was a bug in the way it was consumed from one of the core .Net 5 assemblies. At least so far, I'm extremely impressed.

Tooling

However, the tooling space might be a bit challenging. For instance, I'm using SonarCloud on Magic, and apparently these parts are not working, something you can see on my Actions tabs in my Magic satellite projects. I suspect it'll be something that'll be fixed by SonarCloud pretty rapidly though. In their defence, as I was updating my NuGet packages, most packages from Microsoft had zero downloads, so I assume a lot of .Net developers are following the same process I did today, including hopefully the developers at SonarCloud. BTW, if you haven't checked out SonarCloud you really should. It's an amazing static analysis tool for software developers. They also provide free accounts for Open Sauce projects for those passionate about such things ...

Docker however was dead simple to fix. Below is my entire Docker file for Magic's backend. As you can see, it's a simple update from "5.0" to "6.0", and that was it.

Dockerfile
# Using .Net 6 image from Microsoft
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY backend.csproj .
RUN dotnet restore
# Copy everything else and build website
COPY . .
WORKDIR /app
RUN dotnet publish backend.csproj -c release -o /magic
# Final stage / image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /magic
COPY --from=build /magic ./
EXPOSE 4444
ENV ASPNETCORE_URLS="http://+:4444"
ENTRYPOINT ["dotnet", "backend.dll"]

I've tested everything, multiple times in fact, however if you want to play around with things, you can find a docker-compose file here for Magic that you can use to immediately get things up running. But, if you are extremely dependent upon tooling, you might want to wait until your tool vendors have been able to update their supported SDKs, such as my problems with SonarCloud above illustrates. For Magic though, the entire process took only a couple of hours. 35 projects updated, new release built, Docker images published, and two hours later - Catching, done!

Painless would be my conclusion!

Performance

On the performance side of things, I can easily notice improvements. I haven't measured the differences exactly, since others have done this at great lengths - But everything "feels" much faster, such as generating a key pair with BouncyCastle, running my integration/assumption-tests, etc, etc, etc.

However, according to things I've read about these things, one of the primary performance improvements done in .Net 6 includes significantly improving performance of streams, which Magic is heavily using, so I would expect this to significantly improve both throughput and performance of the average Magic app. Streams have been optimised sometimes as much as 10x in the core of .Net, depending upon your operating system of choice. This is true both for memory usage and CPU usage, in addition to eliminating a whole range of blocking calls, improving throughput obviously for Magic.

C# features

As to most of the new C# features, I'm not all that eager though. In fact, if somebody used the new x..y notation as an alternative to Skip(x).Take(y), I think I'd even flag it as a bug, and refuse to approve pull requests. Personally, it feels as if the C# team has run out of cool features to implement, and most of the stuff they're doing these days is "me too" kind of stuff. As always, using .Net, or any other framework for that matter, is just as much a question about what not to use, as it is about what to use ...

... which arguably was a lot of my motivation to create Magic in the first place!

Conclusion

However, all in all, this is a really good release, and I'm looking forward to replace Newtonsoft.JSON with System.Text.Json, now that I finally have mutable DOM classes that allows me to do such things. I'm also looking forward to be able to inject my README files into my NuGet repositories, I'm happy these fake warnings about Analyzers disappeared, and I am super thrilled about the performance and throughput gains.

However, should you upgrade? Well, that depends upon your tooling. Does your pipeline support .Net SDK version 6? Does your static code analysis tool support .Net 6? Etc, etc, etc. How fast you should upgrade, really depends upon these answers. For me personally, I can for the moment manually execute all my unit tests locally for a while, until SonarCloud comes around and implements support for .Net 6 SDK. However, the answer might be different for you. Regardless, this is a really great release!

Bravo Microsoft!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK