Not every Portable Class Library is equally Portable
Portable Class Libraries are a great way to share common code between different platforms. But here lies the danger: A portable class library project cannot change the underlying platform.
Here’s a simple explanation of what that means.
Example project
The example application is built for Windows Forms, Windows RT and Windows Phone. The authentication mechanism is identical in every app so that is placed in a Portable Class Library project:
public class WebTool { public async Task<string> GetAuthenticationKey() { var cookieContainer = new CookieContainer(); var client = new HttpClient(new HttpClientHandler() { CookieContainer = cookieContainer }); // Authenticate with the service await client.GetStringAsync("http://cookietest.api.domain.com/cookie/get"); // Return the received cookie as authentication key var cookieHeader = cookieContainer.GetCookieHeader(new Uri("http://cookietest.api.domain.com/")); return cookieHeader; } }
Using this library doesn’t get easier: Each platform can call it with identical code:
var webTool = new WebTool(); var result = await webTool.GetAuthenticationKey(); if (string.IsNullOrWhiteSpace(result)) this.Result.Text = "Didn't receive authentication key!"; else this.Result.Text = "Authentication key: " + result;
Few minutes and everything is done, so the testing phase begins.
First, Windows Forms:
Then Windows 8:
And last but not least, Windows Phone:
Uh oh.
Cookies and CookieContainer especially are different beasts in different platforms. Portable Class Library cannot hide that fact.
Have you encountered similar issues when dealing with Portable Class Library projects?