Wednesday, 16 August 2017

Code therapy brought to you this week by .Net Core 2

So the last week has been hard going so tonight I decided to have some code therapy. I've been meaning to explore .Net Core 2 further and decided tonight was the time to port a library I had written in Mono for Raspbian over to .Net Core land.

It's worth noting that .Net Core 2 is only in preview on Raspbian but thats good enough for me ;)

Getting Started

First things first I updated VS2017 to the latest update 3 release. My Visual Studio wasn't telling me the update was available so I simply got the web installer again from VisualStudio.com and it advised VS was already installed and could be updated. This took about 35 mins on my Dell XPS 13 laptop but on my SurfacePro 3 its taken nearly an hour.

.Net Standard Choices
I then chose to create a new .Net Standard project but found I couldn't target .Net Standard 2. Turns out Update 3 doesn't include .Net Core 2 you need to go get this from: https://www.microsoft.com/net/core#windowscmd

You can now choose .Net Standard 2.0

I then created Classic .Net Console Application and a .Net Core Console Application to use my class library in.

Adding a reference for the .Net Standard library in Classic .Net Console Application is easy enough,  References > Add Reference but when it came to .Net Core I had to look twice. In .Net Core References is gone, its replaced by Dependencies. Dependencies nicely wraps up project references, service references and Nuget Packages. So right click Dependencies and you get the famiiar Add Reference.

Where's References Gone... Oh There You Are!

Porting My Library

So to start with I just grabbed the code from my Mono project and dropped it into the new Standard Library, it was easier to avoid any complications by simply copy/pasting the files, I have no doubt I could have messed around with the old project to make it a standard one but why gamble.

I then thought lets do a build and see what doesn't work, its a very simple project, basic models, interfaces, but I do use Process.StartNew new to call out and grab some info using Bash on Raspbian, I expected this to have some quirk or API change.

Turns out it built perfectly, 0 build errors, 0 warnings. Crazy.

Next I implemented a very basic console app, again same code in both applications and ran both applications. Spot the difference....



Same output the only difference is how the app is ran, in the classic world we compile an executable where as an net core app is compiled to a dll which is then ran via the dotnet cli.

Next Steps

I now wanted to get this running on my Raspberry Pi, the classic console application can be ran via Mono, this just got a 5.2 release. For the .Net Core Console app I needed to ensure its compiled for ARM32, to do this I used the .Net CLI and published the application. Rather than repeat how to do this Microsoft have all of this on GitHub.

Conclusion

I'm very impressed with how compatible .Net Core 2.0 is , this was a very small library that ported seemlessly but this really opens up the migration path for many of my other ones and begins to make me think I need to rely on Mono far less. I'm looking forward to the future.



No comments:

Post a Comment