Sunday, April 9, 2017

Xamarin and Azure Storage

June 2022 - This article is probably irrelevant because the Xamarin development platform has changed significantly since this article was published. Portable Class Libraries (PCLs) are deprecated, Xamarin has changed and NuGet packages and dependencies have changed. This post remains here for historical interest.


There may be times when creating, designing and deploying a complete REST web service to support a mobile app might be overkill. Each service I create means more code, more contracts, more installers and more "stuff" to remember and manage.

As I've said in other posts, I'm a fan of Azure Storage tables, blobs and queues because they're so cheap, fast, easy to code, and best of all ... hosted by someone else. So I wondered if a Xamarin mobile app could directly use the Windows Azure Storage Nuget package, thereby using Azure Storage as a general purpose backend. Sadly, hours of experiments and web searching confirmed that a PCL (Portable Class Library) project cannot reference the package. I asked in the official Xamarin forums if there was any way a Xamarin Forms app could use Azure Storage, but after two weeks I never received a reply. With a fresh mind I ran more experiments and found the answer, and I have pasted the resulting forum post below as a reminder to myself and as possible help to others.



In the absence of replies, I think I've worked this out myself.

The WindowsAzure.Storage package cannot be directly referenced in a PCL project. I initially thought that there would be some workaround by adjusting the targets or build options, but the following statement on a Xamarin documentation page confirms I was wasting my time. The statement is strangely worded, especially the first sentence which misleads you into thinking that a Shared Project is mandatory, whereas it should say "the Azure library can be referenced from platform projects and a Shared Project is a convenient way of sharing code between the platform projects."



I found that you can add the WindowsAzure.Storage package to iOS and Droid projects and they work correctly. You can share the Azure code with the two platform projects using a Shared Project, which is what they were designed for. Then you can publish the Azure functionality to portable projects using the Dependency Service feature and an interface. I have never needed to use Shared Projects before, sticking only to PCLs because they are a neater separation of functionality. So now I can see my first real justification for the invention of Shared Projects, although you could also add the common source files to the project as links.

While discussing this issue in my local .NET forum it raised a bit of confusion between the packages WindowsAzure.Storage and Microsoft.Azure.Mobile.Client. The latter can be added to a PCL project, but it's designed to work specifically with Easy Tables, a web service and database schema you compose in a wizard style in the Azure portal. I've seen this done in a live demo and it was quick and easy and would be a great RAD choice if the situation arises. However I was not interested in Easy Tables, I wanted Azure Storage access using the general purpose package I've been most familiar with for a few years.

No comments:

Post a Comment