Scott Hanselman

Updating FloatingGlucose to .NET 5 - Display Nightscout or Dexcom Glucose Values on the Windows Desktop

May 11, 2021 Comment on this post [2] Posted in Diabetes | DotNetCore | Open Source
Sponsored By

Last month I learned how to convert a 13 year old .NET app to modern .NET 5 with the "try convert" tool. It was surprisingly easy to bring a "legacy" app well forward to a pretty darn cool self-contained app. That means NO .NET install. Just run an exe and you're set.

I found this 5 year old .NET application called FloatingGlucose from Bjørn that connects to a Nightscout open source diabetes server like the one I use and displays your current glucose values in a floating popup on your Windows desktop. It can also connect to the Dexcom Share servers. However, the app has an installer and requires .NET 4.6 to be installed.

How quickly - and successfully - can I convert it to a self-contained .NET 5 app? I don't know, I haven't written that part of this blog post yet. I'm still up here.

Let's try it.

Ok, I've forked it here https://github.com/shanselman/FloatingGlucose and I will bring it down to my local machine.

I started using the GitHub CLI lately and it's great, so I'll clone with it.

NOTE: I'm surprised I can't clone AND get submodules with the GH CLI so I guess I need to git submodule update --init --recursive after the initial use of GH.

And looks like GH is out of date so I'll 'winget install gh' to update it while I'm here.

winget install gh

Cool. Now I'll use the dotnet upgrade assistant. This tutorial will show you how. The upgrade assistant is a whole text-based command-line wizard to help you update solutions.

Upgrade Steps

Entrypoint: C:\github\FloatingGlucose\FloatingGlucose\FloatingGlucose.csproj
Current Project: C:\github\FloatingGlucose\FloatingGlucose\FloatingGlucose.csproj

1. [Skipped] Back up project
2. [Next step] Convert project file to SDK style
3. Clean up NuGet package references
4. Update TFM
5. Update NuGet Packages
6. Add template files
7. Upgrade app config files
a. Convert Application Settings
b. Disable unsupported configuration sections
8. Update source code
a. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
9. Move to next project

Choose a command:
1. Apply next step (Convert project file to SDK style)
2. Skip next step (Convert project file to SDK style)
3. See more step details
4. Configure logging
5. Exit
> 1
[22:25:49 INF] Applying upgrade step Convert project file to SDK style
[22:25:49 INF] Converting project file format with try-convert, version 0.7.222801
[22:25:52 INF] [try-convert] Conversion complete!
[22:25:52 INF] Project file converted successfully! The project may require additional changes to build successfully against the new .NET target.
[22:25:55 INF] Upgrade step Convert project file to SDK style applied successfully
Please press enter to continue...

So I'm going step by step through the process, answering questions. OK, done. Let's see if it builds.

Looks like it doesn't. It has a custom setup PowerShell script that is trying to call InnoSetup and make an installer. Since I want to make a version that doesn't require an installer, I can remove all that from the PostBuildEvent and PreBuildEvent in the main csproj file.

Now I'm getting a number of compiler errors that aren't related to the conversion, but rather a misunderstanding about how exceptions are to be re-thrown.

When you say "throw err" you're actually losing your call stack and context.

else
{
//rethrow because we don't know how to handle other errors
throw err;
}

Instead, just:

else
{
//rethrow because we don't know how to handle other errors
throw;
}

throw to maintain the current error context. I'm just tidying up at this point.

I see a few warnings like:

C:\github\FloatingGlucose\FloatingGlucose\FormWebbrowser.cs(10,6): 
warning SYSLIB0003: 'PermissionSetAttribute' is obsolete:
'Code Access Security is not supported or honored by the runtime.'

and I comment them out as they are vestigial at this point.

I'm down to a final error:

Could not resolve this reference. Could not locate the assembly "System.Windows.Forms.DataVisualization".

Interesting, what's this about? Looks like not all of the Windows Forms DataVisualization project came along for .NET 5? The older code is here https://github.com/dotnet/winforms-datavisualization for .NET Core 3. The readme says "We consider the System.Windows.Forms.DataVisualization deprecated and we only provide it to ease porting to .NET Core 3"

However, Angelo Cresta has a .NET 5 version here https://github.com/AngeloCresta/winforms-datavisualization-net5 so I can try updating to his version. I'll bring it in as a Submodule and then add it as a Project Reference from the main FloatingGlucose app.

I'll also bump the JSON.NET reference from 9 to 13.0.1

NOTE: I'll also put in a dotnet_diagnostic.CA1416.severity = none in an .editorconfig to keep the compiler from warning me that this requires Windows 7 and above.

Boom, this 5 year old app is now a .NET 5 app and builds clean. SHIP IT. Well, test and run it first, eh?

2>Done building project "FloatingGlucose.csproj".
========== Build: 2 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

Nice, a clean warning-free build AND it starts up immediately AND looks OK on my 4k monitor likely due to the work that was done to make WinForms apps scale nicely on high dpi systems.

Floating Glucose

Does it work? Darn right it does.

image

Fabulous. Now I have a little widget I can have on my screen at all times that shows my current sugars!

And finally, can I make it a single no-install EXE that doesn't even need .NET installed?

dotnet publish --configuration Release --runtime=win10-x64 --output ./publish -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true --self-contained=true -p:DebugType=None .\FloatingGlucose\FloatingGlucose.csproj

Yes, now I have a single EXE I can run of this now .NET 5 WinForms app:

Floating Glucose

Fab. That was fun. Took about 2 hours. Consider if YOU can update your "legacy" codebase to .NET 5 and reap the benefits! Here is the 'works on my machine' late night release. PR's welcome. Night!


Sponsor: The No. 1 reason developers choose Couchbase? You can use your existing SQL++ skills to easily query and access JSON. That’s more power and flexibility with less training. Learn more.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
May 14, 2021 17:55
Very cool, easy conversion. Thanks for sharing your experience!
May 19, 2021 12:31
"FloatingGlucose" is probably craziest name for software library I've ever seen :)

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.