Sergii Baidachnyi

Blog about technologies

Azure Mobile Services: Visual Studio

with one comment

In my series I used JavaScript backend in order to handle insert operation. Thanks to that I was able to send broadcast notifications to all subscribers. In this example we had small amount of code and using of JavaScript looks reasonable despite of my love in .NET. But in many projects your code might be more complex and you might want to use Visual Studio and C# in order to develop it. Therefore in this post I want to show how to use Visual Studio in order to build our backend.

First of all in order to use C# on the backend, you must select .NET as a backend technology in new Mobile Service dialog.


You may use Visual Studio as well to create Mobile Service but I like management panel. I simply got used to use management panel for several years:)

Right now we are ready to use Visual Studio. In the first step we will create project based on Azure Mobile Service template. You need to provide name of your project/solution there.


In the second step you need to select some options. You should switch off Host in the cloud checkbox. If you forget to do it, Visual Studio will propose to create a new Mobile Service.


Once you click OK, Visual Studio will create a project for you, which contains several ready to use classes like TodoItem, TodoItemController etc. You can use this code in order to deploy TodoItem table to Azure Mobile Service.

I am not going to remove auto-created code but I would prefer my own names for table and columns. So, I will change TodoItem to NotificationData. You can use refactoring tools to change class name. Additionally, I will remove Complete property and change Text to text property. Last changes you may make using JsonProperty attribute as well.

public class NotificationData : EntityData
public string Text { get; set; }

Because EntityFramework and Web API already have all needed infrastructure for our service, we will concentrate our attention around the following questions:

· How to send broadcast notifications from our code;

· How to set permissions for operations with tables;

· How to create own code, which will handle registrations of new devices;

The simplest question there is sending notifications. You need to open TodoItemController.cs file and modify PostTodoItem method in the following way:

public async Task<IHttpActionResult> PostTodoItem(NotificationData item)
NotificationData current = await InsertAsync(item);

WindowsPushMessage message = new WindowsPushMessage();

message.XmlPayload = @"<?xml version=""1.0"" encoding=""utf-8""?>" +
@"<toast><visual><binding template=""ToastText02"">" +
@"<text id=""1""></text>" +
@"<text id=""2"">" + item.Text + @"</text>" +
var result = await Services.Push.SendAsync(message);
catch (System.Exception ex)
Services.Log.Error(ex.Message, null, "Push.SendAsync Error");

return CreatedAtRoute("Tables", new { id = current.Id }, current);

You can see that there we used two classes there. WindowsPushMessages allows to create a toast message and ApiServices (Service instance) allows to send the message and put information to the log. Like in case of JavaScript we used ToastText02 template, which contains title and message there. So, we have the same code but in C#.

Let’s talk about permissions. In case of JavaScript we used Azure Management panel to set permissions but in case of C# we need to use attributes there. You need to use AutorizeLevel attribute in order to set the right permissions to methods on Controller class.

public async Task<IHttpActionResult> PostTodoItem(NotificationData item)
public IQueryable<NotificationData> GetAllTodoItems()

Of course, this approach will work just for table operations but in case of JavaScript you had a chance to set authorization level using Azure Management panel. In case of C# you may set it in WebApiConfig.cs file. Just put the following line of code to Register method:

options.PushAuthorization = AuthorizationLevel.Anonymous;

In our example we didn’t change Registration pipeline but if you want to do it, you can implement INotificationHandler interface.

public class PushRegistrationHandler : INotificationHandler
public System.Threading.Tasks.Task Register(
ApiServices services, HttpRequestContext context, NotificationRegistration registration)
throw new NotImplementedException();

public System.Threading.Tasks.Task Unregister(
ApiServices services, HttpRequestContext context, string deviceId)
throw new NotImplementedException();

Implementation of the interface will be picked up automatically and you may run your own code there.

Finally, in order to test our solution, we need to deploy it. In order to do it just select Publish menu item from context menu and set up all needed parameters:


In order to simplify this process you can download Publishing profile from the dashboard already created service



Written by Sergiy Baydachnyy

01/06/2015 at 10:44 PM

Posted in Microsoft Azure

Tagged with

One Response

Subscribe to comments with RSS.

  1. […] Azure Mobile Services: Visual Studio […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: