Scott Hanselman

Cross-Platform Portable Class Libraries with .NET are Happening

July 10, 2013 Comment on this post [28] Posted in Learning .NET
Sponsored By

imagePortable Class Libraries are the Happy Little Feature that Could. They've been chugging along, doing their thing, and it's getting to the point where they are going to pop. That's pop in a good way.

If you're not writing .NET apps for more than one target, then you likely haven't bumped into them. However for those people who are writing .NET and want it to run on everything from Watches to Phones to Tablets to Xboxen to Desktops to the Cloud, they are enjoying what PCLs can offer.

There's still a few technical and legal challenges but I'm confident they'll be dealt with and we'll be able to create great binary libraries that can be used everywhere.

There's been a bunch of recent activity in the .NET community around Portable Class Libraries and cross platform .NET.  Overall, Portable Class Libraries are starting to see wider adoption, more open source libraries are being released with portable support, and the MVVM pattern is proving to be a great way to maximize code sharing in cross platform apps.

Portable Library Releases

First of all, there have been a bunch of new libraries released as PCLs recently.  Three of them are from GitHub's own Paul Betts:

  • Reactive UI – Reactive UI is an MVVM framework built on top of the Reactive ExtensionsVersion 5.0 was released last week, which is "totally Portable-Friendly", and supports the following platforms:
    • Xamarin.iOS
    • Xamarin.Android
    • Xamarin.Mac
    • .NET 4.5 (WPF)
    • Windows Phone 8
    • Windows Store Apps (WinRT)
  • Akavache - An asynchronous, persistent key-value store.  Version 3.0, which includes PCL support, was also released last week, and now "nearly all of your serialization and network access layer can be cross-platform." Akavache supports the same set of platforms as Reactive UI. This is a really amazing piece of software that deserves its own blog post. I'll do one soon.
  • Splat – "A library to make things cross-platform that should be." It has cross platform APIs for images and colors, with platform-specific extension methods to go back and forth between the platform-specific native types.  This looks like an elegant solution to the problem you run into if you want a portable ViewModel but you want to expose an image in it.

Next, Scott LoveGrove confessed:

I might be getting a little obsessed with PCLs at the moment. #IHaveAProblem (link).

Scott has released four portable libraries that help access web services:

  • Scoreoid Portable - A Portable Class Library that gives developers access to the Scoreoid scoring system.  The library provides a friendly .NET wrapper for the Scoreoid REST APIs, and uses our portable HttpClient NuGet package.
  • FanArt Portable - A Portable Class Library that gives developers access to the fanart.tv film, TV, and music image resources.  It also uses portable HttpClient.
  • Cineworld Portable - A Portable Class Library that gives developers access to the Cineworld film and cinema listings for the UK and Ireland.  It also uses portable HttpClient.
  • LiveSDKHelper - A helper library to more easily use the Microsoft Live SDK.  Contains strong types which the responses from the Live SDK can be deserialized into.

Another new portable library release is Budgie, a library for accessing Twitter. The .NET and Azure Teams have also released a PILE of Portable Libraries.

.NET Team:

Other teams:

Not to mention all these excellent Portable Libraries that you should be familiar with as well.

MVVM

IoC

Other

and of course, my favorite, the HttpClient, making HTTP calls easier, since, well, since a few months ago.

public static async Task<HttpResponseMessage> GetTheGoodStuff() 
{
var httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://hanselman.com/blog/");
var response = await httpClient.SendAsync(request);
return response;
}

//build/

One BUILD attendee took issue with the fact that speakers were telling people that you can't use platform specific functionality from a Portable Class Library, and wrote a blog post about how you can do this with abstraction and (preferably) dependency injection.

On at least 2 occasions recently, I have heard speakers tell their audience that you cannot reference a target-specific .NET library (such as a .NET Framework 4.5 library) from a Portable Class Library. While this is technically true, it doesn't tell nearly the whole story. Even though we can't reference target-specific libraries, we can still USE these libraries. We can call their methods and access their properties under the right circumstances. We can gain access to these libraries via an abstraction. My preferred method of doing this is...Dependency Injection.

Cross Platform .NET Coolness

There's lots of great examples of code reuse, like the podcast I did with the folks who wrote "Draw a Stickman EPIC." This app has 95%+ code reuse, all written in C# and is available in every App Store there is. Here's a few others.

Also, be sure to check out my talk from the Xamarin Evolve Conference - How C# Saved My Marriage. I talk about Portable Libraries in this talk. We'll hear more about Portable Libraries at the MonkeySpace Conference in Chicago in July of 2013.

clip_image001[6]

The British and Irish Lions app is a cross platform app targeting Windows Store, Windows Phone, iOS and Android. It uses Portable Class Libraries to share common code across platforms, Azure to host the services it uses, and the MvvmCross MVVM framework which I'm a huge fan of. The UK MSDN blog posted a case study of the development of the app, and there is also a prettier case study (with screenshots, infographics, etc) and a blog post by the developers.  In reference to Xamarin, MvvmCross, and Portable Class libraries, they state "Given the very short time-scales for the Lions apps (less than three months from the first line of code to our first release), we simply could not have delivered a rich native experience across all platforms without the common core."

Lions Rugby

Another cross platform app that uses MvvmCross is Aviva Drive.  This is an insurance company app that you can use to track your driving habits and hopefully get a discount on your insurance.  The app was featured in the Tech Ed Europe day 1 keynote (about 28:40 in), focusing on how it used Azure.

On the game side of things, Taptitude is a successful Windows Phone 7 game (or rather, a collection of minigames) which was ported to Windows Phone 8, Windows Store, iOS, and Android with the help of MonoGame and Xamarin.  The team reports that they have more than 99% shared code between all the platforms, which is especially important to them because they ship updates to the game about every week.

Xamarin

Scan.Xamarin.com

Xamarin has released an amazing .NET Mobility Scanner which analyzes code and tells you how "portable" it is and how compatible it will be with Xamarin.Android, Xamarin.iOS, Windows Phone, and Windows Store.  It lists all the APIs your code uses that aren't available on all the platforms, and an overall percentage of how portable your code is.  It's a very slick implementation and similar to what we've wanted to have for Portable Class Libraries.  Here's a sample report for the SignalR client library. The best part is that it does all this analysis without sending your code or binaries to Xamarin. It all happens in the browser. I love Xamarin.

Phil Haack (former PM for ASP.NET MVC and now working at GitHub) wrote a blog post titled Platform Limitations Harm .NET.  He argues that the Windows platform limitations should be removed from the EULAs for BCL NuGet packages.

I personally agree. Being able to reuse existing code, make portable libraries, and write apps that run in 64k or 64gigs is what makes .NET an interesting platform.

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
July 10, 2013 1:59
Portable Class Libraries bring with them some interesting artifacts that look odd when you open them in your decompiler of choice. They also have some trouble on machines that don't have the latest .NET patches on them.

I wrote up some Portable Class Library FAQs in response to common questions we get with Autofac. If you're publishing a PCL, it's good to know what you might run into.
July 10, 2013 2:38
Nice one, just in time for my tryout with xamarin. That http client is great I used it with porting windows 8 code to WP 8. A real timesaver
July 10, 2013 2:53
Scott,

How does the Eula of pcl apply to Reactive UI? As Phil says, you're not allowed, aren't you?
July 10, 2013 3:27
Travis: Thanks. Just a correction on your FAQ, you might be confusing .NET 4.0 behavior with .NET 4.5. You are right in that .NET 4.0 does require KB2468871 to be installed for portable libraries to run, however, .NET 4.5 supports them out of the box.

Oscar: There is some confusion; Phil Haack's blog is talking about our BCL packages that are built on top of .NET (using portable). These EULA's only apply to these packages (Microsoft.Net.Http, for example). There are no restrictions from Microsoft around what platforms 3rd-party portable libraries support.
July 10, 2013 5:45
26 - 16... I wish that's all the final score was in the final Lions vs Wallabies game.
July 10, 2013 9:22
Thanks for the shoutouts Scott! I actually just pushed out a new release of Splat today that adds support for cross-platform geometry primitives (i.e. Points, Rects, and Sizes). Not necessary for every app, but when you need it, not having it is **super** painful.

Oscar: All three of my libraries above are licensed under permissive licenses (RxUI is licensed under MS-PL, and both Akavache and Splat are under the MIT license). Reuse it wherever you want :)
July 10, 2013 10:44
Nuget.org search and filtering by PCL should be added. It's tricky to discover a PCL.
July 10, 2013 17:05
According to this link , it is not permitted to use them on other operating systems , so what is the point from them then

http://haacked.com/archive/2013/06/24/platform-limitations-harm-net.aspx?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+haacked+%28you%27ve+been+HAACKED%29
Sam
July 10, 2013 18:10
Sam, look at the update, they messed up in the legal department.
Fan
July 10, 2013 21:42
Thanks man I am trying this out with Xamarin now. Should prove to be useful!
July 11, 2013 1:21
We already have "one asp.net". Now we are getting close to "one .net".

If only PCL in the Micro Framework were possible. That would be great. PCL is probably not the problem but MF is.
I'm hoping we can get a complete new Micro Framework that aligns with the actual .net framework (and PCL).
July 11, 2013 1:55
Sam: Just to be very clear, Phil Haack is talking about a few specific binaries produced by my team. He is not talking about portable libraries in general. There are no restrictions with regards to portable libraries and what platforms they can run on.

David Kean
BCL Team
July 11, 2013 9:35
Taking bets on how long it will take for a "JavaScript" checkbox to appear on that dialog.
July 11, 2013 15:30
Is there a guide on how Portable Libraries are implemented and what does it take to add another custom platform.?

One can use Saltarelle C# to Javascript compiler in combination with such a platform to output Javascript :0
July 11, 2013 16:00
Thumbs up for the love for PCL's!
I have used them in a few projects, and they can be very handy, especially with the new libraries rapidly coming out. Although very handy I have often noticed a few shortcomings. Also, they are not the only way to share code between platforms. I recently wrote a few articles with some tips on how to share code and when to choose what(especially for Windows Phone and Windows 8, but it can be extended to all platforms):

Portable Class Libraries or source code sharing

Patterns for sharing code in Windows Phone and Windows 8 applications
July 11, 2013 16:36
Hmmm, no mention of windows CE 6.5 -- any chance?
July 11, 2013 20:57
"Rich native experience"
Screenshot of an Android app with a bottom iPhone "navbar."

Hahaha.
July 11, 2013 21:12
@David Kean: so every one in your team can change the legal agreement as they desire ?
Sam
July 11, 2013 21:13
why there is no one agreement that all assemblies inherit it , that will make the life easier for developers and companies
Sam
July 12, 2013 1:02
@David Kean: Nice catch; updated my FAQ to say ".NET 4.0" instead of ".NET 4.5" on that answer.
July 12, 2013 14:13
Hi Scott,

Do you have performance benchmark C# vs F# vs Java Similar to this below URI
http://reverseblade.blogspot.sg/2009/02/c-versus-c-versus-java-performance.html

Several source state that .Net + Mono + Apache + Linux is faster than
.Net + VS + IIS + Win

July 16, 2013 15:01
I come back with a question:

Is there any documentation on how to develop a platform for portable libraries?
I did not find any information about the internals...

Regards
July 17, 2013 1:57
I spoke at a user group last week and took five minutes aside to make a point about PCLs being the future for multi-plat development. I can now take this post and rub it in the face of that one co-worker who disagreed!

It all came from a MVVM project done using Ninject & JSON.NET with everything below the View layer in PCLS shared between a Windows 8 view project, a Windows Phone view project and then eventually a MVC website project. Now that PubSubEvents and Unity are PCLs, the real party is on!

Any word on Xamarin and PCLs? Building a single ViewModel layer shared between iOS, Android and Windows Phone would be amazing. I tried the hack that floated around last year and it didn't seem to work too well.
July 18, 2013 7:36
Another great library to add to your list is Noda Time (www.nodatime.org), an excellent date/time library. The PCL version is a separate build, but it work great!
September 12, 2013 19:41
Hi Scott, unfortunately that wasted space you refer to can't be used until MS changes the licensing on many of their Nuget components (e.g. HttpClient) as these stipulate that they have to be used on Windows systems... making them far from portable!

Are you aware of this, and do you know whether this situation will change in the near future?
September 13, 2013 3:17
JasonBSteele - Yes, we're trying to remove that legal stuff as fast as we can. http://codebetter.com/howarddierking/2013/07/23/katana-license-lifts-windows-only-restriction/
October 28, 2013 17:02
Licensing Changes:
http://blogs.msdn.com/b/dotnet/archive/2013/10/14/portable-class-library-pcl-now-available-on-all-platforms.aspx
Yay! :)
November 14, 2013 18:37
And now...
http://blogs.msdn.com/b/dotnet/archive/2013/11/13/pcl-and-net-nuget-libraries-are-now-enabled-for-xamarin.aspx

Comments are closed.

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