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
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:
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:
- public class Book : EntityData
- {
- public string Title { get; set; }
- public string Author { get; set; }
- public string Description { get; set; }
- public int Cost { get; set; }
-
- public bool Complete { get; set; }
- }
Now, we will add code for the Controller for Book class, as in the following code snippet:
- public class BookController : TableController<Book>
- {
- protected override void Initialize(HttpControllerContext controllerContext)
- {
- base.Initialize(controllerContext);
- MobileServiceContext context = new MobileServiceContext();
- DomainManager = new EntityDomainManager<Book>(context, Request, Services);
- }
-
-
- public IQueryable<Book> GetAllBooks()
- {
- return Query();
- }
-
-
- public SingleResult<Book> GetBook(string id)
- {
- return Lookup(id);
- }
-
-
- public Task<Book> PatchBook(string id, Delta<Book> patch)
- {
- return UpdateAsync(id, patch);
- }
-
-
- public async Task<IHttpActionResult> PostBook(Book item)
- {
- Book current = await InsertAsync(item);
- return CreatedAtRoute("Tables", new { id = current.Id }, current);
- }
-
-
- public Task DeleteBook(string id)
- {
- return DeleteAsync(id);
- }
- }
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":
- public DbSet<Book> Books { get; set; }
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:
- protected override void Seed(MobileServiceContext context)
- {
- List<Book> Books = new List<Book>
- {
- new Book { Id = Guid.NewGuid().ToString(), Title="First", Author = "Abd", Complete = false },
- new Book { Id = Guid.NewGuid().ToString(), Title="Second",Author = "Abd", Complete = false },
- };
-
- foreach (Book Book in Books)
- {
- context.Set<Book>().Add(Book);
- }
-
- base.Seed(context);
- }
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: