Wednesday, May 1, 2013

Enable Azure Mobile Services for "pure" Windows Phone 7.1 Xna Games

     Recently somebody asked me for an advice on what library should he use to communicate with an Azure Mobile Service for an Windows Phone OS 7.1 and Windows 8 Xna game. My answer was, of course, the Windows Azure Mobile Services client library available on NuGet as I remembered that the prerelease version works also for Windows Phone OS 7.x applications.

The "problem" is that the library will not install if the game is a pure Xna Windows Phone project.

There are 2 (maybe 3 :) ) solutions to this problem:
1. Migrate your XNA project to an "Windows Phone XAML and XNA App" and on this type of project the client library will install correctly.
2. You could use a small hack in order to force NuGet to install the Azure Mobile Services client library on your XNA project. The hack is pretty simple:
  • open the YourProject.csproj file with an text editor (like Notepad) and add these 3 line (not sure if all 3 of them are needed but doesn't matter as it is only done to force the library to install):
  •  <SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>  
     <TargetFrameworkProfile>WindowsPhone71</TargetFrameworkProfile>  
     <TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>  
    
  •  Visual Studio will see that the project has been modified so it will ask to Reload the project. Reload it (don't try to build the project as it won't), open the Package Manager Console and write:
     Install-Package WindowsAzure.MobileServices -Pre   
    
    the package should now install.
  • Reopen the .csproj file in Notepad and remove the 3 lines we've previously added, save and reload the project in Visual Studio. The project should compile correctly and you should be able to use the Azure Mobile Services client library.

The only thing that doesn't seem to work is make Visual Studio recognize the await keyword for the XNA project. You will have to use the ContinueWith syntax:

 void TestAzureMobile()  
     {  
       todoitem item = new todoitem { Text = "Awesome item XNA" };  
       ms.GetTable<todoitem>().InsertAsync(item).ContinueWith((task)  
         =>  
       {  
         //do what you need here  
       });  
     }  

3. Came to me while writing the post. Guess the most "elegant" way is to add an Windows Phone Class library to your project, add and use the Azure Mobile Service client library from the class library, reference the class library from your XNA project. Tried this one but the MobileServiceClient seems to fail initialization with 'System.TypeInitializationException'.

Take in consideration that the Azure Mobile Services client library is still a pre-release version in this moment so for the release they might fix/enhance it and you could install it on XNA projects without any hack needed (it is a PCL library).

5 comments:

  1. I guess installing the Microsoft.Bcl.Async from NuGet using the same trick for enabling the async syntax won't work, right?

    ReplyDelete
  2. I also agree with you ..... very useful information for us ...... keep it up thanks for this..........






    Mobile Development

    ReplyDelete
  3. It seems like this hack doesnt work anymore. I'm using visual studio 2012 going for XNA with Windows Phone.

    When I add the 3 lines i'm able to install mobile services..
    I save and reload and remove those 3 lines and all of a sudden Azure Mobile services remove's it self.

    Do you have another way of adding Mobile services to raw XNA games I dont like the XAMl & Xna setup.
    Thanks.

    ReplyDelete
  4. What a lovely idea. I like your details and info. Thanks for sharing this helpful idea. cep telefonun

    ReplyDelete