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.
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.
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.
Now in the solution explorer, right click and go to 'Properties'.
Here you will see an option to “Target .NET Platform Standard”. Go ahead and chose this.
And that’s it!
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 dependencies, you should see .NetStandard listed:
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.