11

I updated to latest version of Xamarin, where PCL is "fully supported". How can i use Azure Mobile Services now?

if i create PCL lib and trying to add it with NuGet it failed to install "Newtonsoft.Json 5.0.8" because it needs "portable-net45+wp80+MonoAndroid10+MonoTouch10" which is missing.

if i create simple android lib and add Azure from components it's generate excepton:

System.InvalidOperationException: A Windows Azure Mobile Services assembly for the current platform was not found. Ensure that the cur…

on

 public static MobileServiceClient MobileService = new MobileServiceClient(
    "https://no-link-here.azure-mobile.net/",
    "no-key-here");    
xakpc
  • 1,709
  • 13
  • 27

4 Answers4

6

First, make sure you are using version 1.1.5 or above of either the NuGet or the Xamarin Component as previous versions had some issues with PCL support.

For a step by step guide on using Mobile Services in a PCL, see this tutorial.

If you install the Xamarin Component or the NuGet package for Azure Mobile Services into your Android application, you will find it adds these references (among others):

  • Microsoft.WindowsAzure.Mobile.dll
  • Microsoft.WindowsAzure.Mobile.Ext.dll

The first assembly is a PCL, the second assembly has platform specific implementation (and that is what you are missing with the error message above).

The NuGet package for Mobile Services has specific platform targets defined for Xamarin.iOS and Xamarin.Android so if you are using Visual Studio, you have the option to use just the NuGet and skip the Xamarin component completely.

Due to some bugs in how the BCL NuGet packages (HttpClient, Bcl.Build, etc) interact with Xamarin, there are a few workarounds you'll need to be aware of. See the tutorial above for the necessary steps.

Paul Batum
  • 8,165
  • 5
  • 40
  • 45
  • wait a minute, i dont get it. Do you mean i can't install Azure Mobile Services to my "portable-net45+wp80+MonoAndroid10+MonoTouch10" PCL library right now? Should i use plain old method with linked projects (files) and references from "Component store" then? – xakpc Nov 27 '13 at 15:21
  • 1
    No, you don't need linked project files. I just posted a tutorial, might help clear things up. If you have more questions email me at pbatum@microsoft.com https://github.com/paulbatum/mobile-services-xamarin-pcl – Paul Batum Nov 28 '13 at 04:18
  • Cool, that's extremly usefull. Thank you! – xakpc Nov 30 '13 at 22:29
  • can you please help me with one more problem @paul. If i use your project or take project from Azure it works perfectly fine, but if i create new android project, add component and same code it fails with exception: System.Security.Cryptography.CryptographicException: Store Root doesn't exists. I think i missing some point.. – xakpc Nov 30 '13 at 23:33
  • I have not seen this error myself, but I did a quick search and maybe you are hitting this issue? http://stackoverflow.com/questions/19309556/how-to-ignore-error-system-security-cryptography-cryptographicexception-store – Paul Batum Dec 02 '13 at 01:50
  • Sad for me, this little hack didn't help at all :( – xakpc Dec 03 '13 at 18:39
  • Can you give me a few more details about the steps you are taking and the code you are using? I would like to try to reproduce the error if possible. Put them on a pastebin/gist. – Paul Batum Dec 04 '13 at 07:30
  • This is a great discussion. But... There should be a new question posted for the crypto exception, otherwise the question (and answer) will be buried in comments (with no way to mark an answer for future reference). – David Makogon Dec 04 '13 at 22:02
  • Looks like that when including WP8 in the PCL, the profile is no longer profile7, and therefore the component's DLL can't be refernced. – Liel Dec 20 '13 at 05:23
  • We've released a new version (1.1.5) of the NuGet package and Xamarin Component and I've updated the above answer accordingly. – Paul Batum Mar 27 '14 at 17:58
6

Make sure you have called

CurrentPlatform.Init(); 

before instantiating the mobile service client

Magnus Ahlin
  • 655
  • 1
  • 6
  • 18
  • Also, if you're using Visual Studio, you may have better luck using the Azure Mobile Services package directly from nuget, rather than adding the Xamarin components. At least that was my experience. – Timothy Lee Russell Oct 31 '14 at 03:14
4

Here's how I got Mobile Services v1.3.2 working in new Xamarin.Forms PCL project:

  1. Install 'Mobile Services’ into shared project ‘Packages’ folder as well as .iOS & .Droid ‘Packages' folders.
  2. Delete duplicate references in .iOS / .Droid folders: “System.Runtime”, “System.IO”, “System.Threading.Tasks"
  3. Remember to add CurrentPlatform.Init(); to ‘AppDelegate.cs' in .iOS project and to ‘MainActivity.cs' in .Droid project.

Paul Batum has done a good sample Xamarin PCL app showing syncing between iOS and Android app: https://github.com/paulbatum/fieldengineerlite

David Douglas
  • 10,377
  • 2
  • 55
  • 53
  • 1
    Don't you mean PCL instead of Shared Project in step 1? Since Shared Projects aren't supporting Components at the moment. – Mittchel May 19 '15 at 07:19
  • Yes to clarify I'm referring to the actual folder which doesn't have a suffix .iOS or .Droid. This is where you 'share' your c# & xaml (Xamarin.Forms) that works cross platform. – David Douglas May 19 '15 at 10:19
  • If you get an error to the effect of "The type System.Net.HttpMessageHandler is defined in an assembly that is not referenced" in your iOS project after following the steps above, add a reference to System.Net.Http using the Add References option. It will look like the HttpClient Nuget package is installed, but unfortunately it isn't compatible with iOS projects. You have to use the mono one from Add References instead. – smlync Jun 24 '15 at 01:01
1

Make sure you've installed the latest version of NuGet. That should fix the issue where Newtonsoft.Json won't install in the PCL.

For Azure Mobile Services, if you still have trouble I'd recommend following the steps in the getting started guide to download a preconfigured sample project.

Daniel Plaisted
  • 16,674
  • 4
  • 44
  • 56
  • thank you very much for your answer. is PCL the only proper way to create cross platform libraries for now? I had a platfrom specific reference in old library. Should i use dependencies like "PCL -> Android platform lib -> Android app" now? – xakpc Nov 26 '13 at 20:01
  • btw, i was forced to reinstall NuGet, there was no any updates – xakpc Nov 26 '13 at 20:02
  • @xakz PCLs aren't the only way to create cross platform libraries, you can still create separate libraries for each platform and share code with linked files. I discuss some of the pros and cons here: http://stackoverflow.com/questions/13797385/what-is-the-advantage-of-using-portable-class-libraries-instead-of-using-add-as/13808655#13808655 You can't reference platform libraries from PCLs (since it wouldn't work on all platforms). To solve this, use the abstraction pattern. See here: http://blogs.msdn.com/b/dsplaisted/archive/2012/08/27/how-to-make-portable-class-libraries-work-for-you.aspx – Daniel Plaisted Nov 26 '13 at 21:18