Scott Hanselman

Running Microsoft Edge on Linux with WSLg while running Visual Studio 2019 and debugging a Linux .NET app with WSL on Windows 10

May 25, 2021 Comment on this post [2] Posted in VS2019
Sponsored By

How about that insane blog title?

You can do lots of cool things with WSLg, which allows you to seamlessly and cleanly run Linux apps on Windows. I've written about this before.

And you know that you've been able to run Linux server apps on Windows for a long time. There's even support in VS Code and VS2019 to debug those apps.

But how far can we take this? What about Debugging a .NET web app running under Linux while running Visual Studio 2019 for Windows and accessing that web app via a Linux Browser?

Why? Why the heck not? Seriously though, because choice and flexibility. If this solution isn't interesting to you, then perhaps you don't have this problem. But if you do have this problem, then here's the solution. Welcome!

Prerequisites

At some point soon, WSL and WSLg will be a part of the mainline of Windows, but at the time of this writing they are inside Windows 10 Insiders 21362+. Follow the instructions here to setup WSL2 and WSLg. This assume you're running a distro like Ubuntu.

Then add a browser like Edge for Linux or Chrome for Linux as below.

## Google Chrome
cd /tmp
sudo wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt install --fix-broken -y
sudo dpkg -i google-chrome-stable_current_amd64.deb

## Microsoft Edge Browser
sudo curl https://packages.microsoft.com/repos/edge/pool/main/m/microsoft-edge-dev/microsoft-edge-dev_91.0.852.0-1_amd64.deb -o /tmp/edge.deb
sudo apt install /tmp/edge.deb -y

You'll know they are working and installed when the Linux GUI apps show up in the Windows Start Menu.

Ubuntu GUI Apps

Adding a Linux Browser to Windows Visual Studio 2019

Open up Visual Studio 2019, and either open up or create a Web Application. From Debug button there's a dropdown (chevron) where you access this menu:

Browse With

Select "Browse With..."

From the Browse With dialog, you're going to add a new Browser, selecting "C:\Windows\System32\wslg.exe" as the Program and "~ -d Ubuntu /usr/bin/microsoft-edge-dev" as the Arguments. Ignore any errors.

Browse With, adding a Browser

You should see the new Browser inside Visual Studio 2019 now and can select it like any other browser.

Microsoft Edge under WSLg under Ubuntu

Boom. Here I am running my Podcast website under Linux on .NET 5 on the server-side AND on the client-side in the Edge Browser as a Linux GUI app!

Debugging Linux Edge GUI app under WSL2 inside Visual Studio 2019

Enjoy! And please watch the BUILD 2021 Application Development Keynote, I think you'll enjoy it.


Sponsor: Build your apps where your customers are. Oracle for Startups delivers enterprise cloud with no lock-in so you can go after any customer—confidently. 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

Introducing The .NET Coding Pack for VS Code - Getting Started with C# for Beginners

May 20, 2021 Comment on this post [8] Posted in Learning .NET
Sponsored By

Are you trying to learn to code? Or perhaps you're an educator or a student, or you know someone who us? Sometimes it's intimidating when you consider all the things to install and run to get started.

Well, we've created a series of all-in-one installers - coding packs - that will set you up in Python, Java, or as of today - C# and .NET!

Check out http://dot.net/learntocode and https://code.visualstudio.com/learntocode

I'm happy to announce our preview of the .NET Coding Pack. You can grab it at http://dot.net/learntocode! We have a .NET Coding Pack for Windows and a .NET Coding Pack for Mac.

When you're done installing, just press Finish and Visual Studio Code will launch into this .NET Interactive Notebook!

It's like Microsoft Word, except some of the paragraphs are words and some of the paragraphs (called "cells") are code you can run! 

Getting Started with C#

Consider this a Beta Release as we want feedback and bugs from you!

We'll be developing notebooks and courseware but more importantly YOU can also make .NET Interactive Notebooks and share them on GitHub!

Add Code Cell

You can add Markdown (text) or add Code, and then share them with your friends or students!

NOTE: If you are a pro dev and already have Visual Studio or Visual Studio Code installed, you don't need to get this coding pack. The pack installs these things, locally, as anon admin user. You can just add the ones you don't have.

  1. Install the latest Visual Studio Code.
  2. Install the latest .NET 5 SDK
  3. Install the .NET Interactive Notebooks extension from the marketplace.
  4. Download our Getting Started with C# Notebook and open it in VS Code.

Give it a try at http://dot.net/learntocode and report issues here https://github.com/dotnet/vscode-dotnet-pack

Also be sure to scroll down on http://dot.net/learntocode as there are TONS of videos and docs to help you on your journey! We have courseware and training at https://docs.microsoft.com/en-us/learn/dotnet/ and a community here to help https://dotnet.microsoft.com/platform/community.

Nearly every day we have YouTube shows with real humans talking about the things they're making with .NET! Check that out at https://dotnet.microsoft.com/live and subscribe to our YouTube https://youtube.com/dotnet.


Sponsor: Extend your runway and expand your reach. Oracle for Startups delivers enterprise cloud at a startup price tag with free cloud credits to help you reel in the big fish—confidently. 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

Register for Microsoft BUILD 2021 free! And check out the Developer Keynote on Day 2

May 19, 2021 Comment on this post [43] Posted in Learning .NET
Sponsored By

Hey friends! Microsoft BUILD 2021 is upon us. You can register for BUILD 2021 FREE here and join us online! I'm going to be giving the Developer Keynote on Day 2 in this segment (around 9am PST) and I can guarantee you've never seen anything like it. Here's a sneak peak of the FIRST 60 SECONDS of the Day 2 Developer Keynote!

ACTION: Register free for BUILD 2021 here! Why register? You don't have to, you can just visit the site and watch the stream if you like, but if you register you can ask questions LIVE and interact LIVE with the team!

Also, be sure to sign up Microsoft Build Cloud Skills Challenge! You complete modules in Microsoft Learn, learn some cool stuff AND (believe it or not) you can win a meeting with me! How weird is that? Someone thought I'd be a prize to someone. My wife is unimpressed. Regardless, you can win meetings - consultations if you're fancy - with me and other Microsoft Engineers, so sign up today! 

BUILD 2021

Here's my personal schedule for the America's Timezones and if you're on the other side of the planet, here is a BUILD schedule optimized for YOUR Timezones!

See you soon! I miss y'all and I hope you and your families are safe and OK.


Sponsor: Extend your runway and expand your reach. Oracle for Startups delivers enterprise cloud at a startup price tag with free cloud credits to help you reel in the big fish—confidently. 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

Exploring the Visual Studio Code REST Book extension for native Notebooks

May 13, 2021 Comment on this post [3] Posted in Open Source
Sponsored By

My co-worker Tanha Kabir has a cool Visual Studio Code extension called REST Book. Sure, there's lots of extensions and lots of ways to call REST APIs from Visual Studio Code - not to mention the command line.

However, REST Book is a Postman-like notebook that uses the Native Notebooks UI experience that's already built into Visual Studio code.

If you already love the concept of Notebooks like I do, you'll love REST Book. It's electric paper! Just like .NET Interactive or Jupyter Notebooks, it mixes Prose and Code in a very clean and very intuitive interface.

NOTE: Today the REST Book extension requires Visual Studio Code Insiders, but not for long. Get Insiders and run it side-by-side with a stable VS Code here.

Here's me calling into my Nightscout server to see my real-time Blood Glucose:

Nightscout in REST Book

Pretty cool. It's early days but I think Tanha already has a very interesting combination of a native Notebook experience and a Postman-like experience. Note I've also created a "SECRET" in the form of my API Key, and then I can refer to it in the GET request using $SECRETS.name.

You can also assign variables like:

let foo = GET https://hanselman.com

and then use them in subsequent cells with $.foo.headers.User-Agent, etc.

Just install the Rest Book extension in VS Code Insiders, and make a new empty text file with a .restbook extension.

You can do GET and have the URL on multiple lines if you split on a ? or &

GET https://www.google.com
?query="hanselman"
&page=2
User-Agent: rest-book
Content-Type: application/json

And you can POST and each new line will be a Header, until the last lines after a line separator which is the body of the call.

POST https://www.myexampleapi.com
User-Agent: rest-book
Content-Type: application/json

{
name: "Hello",
text: "Hi friends, how are you all?"
}

If you want to get involved, look at the source, file an issue or bug, or just give compliments, do it over on her repository at https://github.com/tanhakabir/rest-book Have fun!


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

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

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