Microsoft Azure Mobile Service In Visual Studio 2015

In this post, we will create a basic Microsoft Azure Mobile Service using Visual Studio 2015 IDE. By using Visual Studio IDE, we can easily create an Azure Mobile service on localhost.

For creating a Mobile Service using VS IDE, we will create a new project in Visual Studio as in the following:

Go to File, then New Project and click Installed Templates. After that under Visual C#, select Cloud, then click Azure Mobile Service

azure3 

Now, if you have an Azure account, you have to login and add the details for the Service. The detail page looks like the following:

azure1

Now, after adding details our project will be created and it will take some time to prepare the application for the use. As our application is created, we will delete the sample ToDoItem class along with it's controller from the solution, and we will add our user defined class. Here, in our solution, we are adding a class Book to the solution:
  1. public class Book : EntityData  
  2. {  
  3. public string Title { getset; }  
  4. public string Author { getset; }  
  5. public string Description { getset; }  
  6. public int Cost { getset; }  
  7.   
  8. public bool Complete { getset; }  
  9. }  

Now, we will add code for the Controller for Book class, as in the following code snippet: 

  1. public class BookController : TableController<Book>  
  2. {  
  3. protected override void Initialize(HttpControllerContext controllerContext)  
  4. {  
  5. base.Initialize(controllerContext);  
  6. MobileServiceContext context = new MobileServiceContext();  
  7. DomainManager = new EntityDomainManager<Book>(context, Request, Services);  
  8. }  
  9.   
  10. // GET tables/Book  
  11. public IQueryable<Book> GetAllBooks()  
  12. {  
  13. return Query();  
  14. }  
  15.   
  16. // GET tables/Book/48D68C86-6EA6-4C25-AA33-223FC9A27959  
  17. public SingleResult<Book> GetBook(string id)  
  18. {  
  19. return Lookup(id);  
  20. }  
  21.   
  22. // PATCH tables/Book/48D68C86-6EA6-4C25-AA33-223FC9A27959  
  23. public Task<Book> PatchBook(string id, Delta<Book> patch)  
  24. {  
  25. return UpdateAsync(id, patch);  
  26. }  
  27.   
  28. // POST tables/Book  
  29. public async Task<IHttpActionResult> PostBook(Book item)  
  30. {  
  31. Book current = await InsertAsync(item);  
  32. return CreatedAtRoute("Tables"new { id = current.Id }, current);  
  33. }  
  34.   
  35. // DELETE tables/Book/48D68C86-6EA6-4C25-AA33-223FC9A27959  
  36. public Task DeleteBook(string id)  
  37. {  
  38. return DeleteAsync(id);  
  39. }  
  40. }  

Now, we have our Book Class and BookController Class in the solution. We will now open MobileServiceContext.cs file, where we will add DbSet of our class "Book":

  1. public DbSet<Book> Books { getset; }  

After adding code in the MobileServiceContext.cs file, we will open WebConfig.cs file where we will make some changes in the code which was actually set for our ToDoItem class object and it's controller to our Book class and it's controller. The changes to be made are in the following code:

  1. protected override void Seed(MobileServiceContext context)  
  2. {  
  3. List<Book> Books = new List<Book>  
  4. {  
  5. new Book { Id = Guid.NewGuid().ToString(), Title="First", Author = "Abd", Complete = false },  
  6. new Book { Id = Guid.NewGuid().ToString(), Title="Second",Author = "Abd", Complete = false },  
  7. };  
  8.   
  9. foreach (Book Book in Books)  
  10. {  
  11. context.Set<Book>().Add(Book);  
  12. }  
  13.   
  14. base.Seed(context);  
  15. }  

In the above code, we are adding some data to our Book class list which can be sent via response when a person calls the GET method of the service. This is only the demo data, you can push your own data and pull that data from the API using GET method.

Now, we will publish the application on to the Azure server to save local changes in the application to the Azure server. Just follow the steps to publish the data and click Publish.

After publishing data, we can check our published service by using GET method of the Mobile service on a Web Browser (you can use Fiddler too for checking GET method of Azure Mobile Services API). When I run my API to GET data in json, it shows data like the following screenshot:



Now, you can also create your Azure mobile services locally on Visual Studio and publish it later on to Azure Server. For parsing the json string from API to your Universal Windows Application check the following blog post:

Up Next
    Ebook Download
    View all
    Learn
    View all