Our Thoughts

If You Need To Go Cross-Platform, Start Using .Net Standard

Posted by Kris Coleman on Feb 1, 2017 11:00:00 AM
Find me on:

In the modern age, software solutions can span across many platforms. One monster solution alone could contain a range of web projects, web services, desktop applications and mobile applications. If you’ve ever written a class library for .Net Framework and later had to target Mono to use it with iOS, you might want to save yourself from the trouble in the future. You might also be one of the lucky few who have worked with PCLs (Portable Class Libraries) and had the pleasure of dealing with those cumbersome PCL profiles. Well no more! PCL is dead, and long live the new King: .Net Standard

I’ve started to create all my new class libraries as .Net Standard as a new best practice. Setting it up is a breeze, and the benefits of cross-platform are self-evident. I can create a library of business objects for a web app, and later develop a mobile app to integrate with the existing web app using the same library, effortlessly. No more PCL Profile woes! In this post, I’m going to walk you through creating your own .Net Standard Library.

 

Step 1: In Visual Studio, create a new Portable Class Library project.

Surprisingly, Microsoft has not yet created a Project Template for .Net Standard, but they have given us a convenient way of converting our PCL’s to .Net Standard.

Start your project by creating a new PCL.

New Portable Class Library to Convert to .Net Standard

 

Step 2: Select some target platforms for the PCL.

Go ahead and select a few target platforms to create the project. Don’t worry, these options are only preliminary and won’t matter after we convert to .Net Standard.

Selecting target platforms for Portable Class Library

 

Step 3: Convert the PCL to .Net Standard

Now in the solution explorer, right click and go to 'Properties'.

Properties for Portable Class Library

Here you will see an option to “Target .NET Platform Standard”. Go ahead and chose this.

And that’s it!

Convert Portable Class Library to .Net Standard

Now you can use this library on any platform for any project!

 

A few things to note:

1. Many third party libraries are already converting to .Net Standard, but not all. If you plan on using third party libraries, I suggest double checking that they depend on .Net Standard. In the Nuget Package Explorer, when looking at a Nuget package’s dependancies, you should see .NetStandard listed:

Note.png

I’ve found that many of my favorite Nuget Packages, like Autofac and Nunit, have already converted.


2. Just about everything you’d normally expect to be a part of .Net is included in .Net Standard, but expect some differences. Working with reflection in .Net Standard can work a little differently, for example. You may have to make some additional calls like “GetTypeInfo()” or “GetRuntimeProperties()”, which you normally wouldn’t have to in vanilla .Net Framework.

 

For more information, I suggest reading this quick article from Yet Another Chris.

 

A Developer's Perspective on Slack for Business: Read More 

Topics: Development Trends

Subscribe to Email Updates

Recent Posts