In the previous chapter, we learned about the Razor templating engine and how you can use it to create a hybrid mobile solution. You learned how to create and use models within your application, and how to save the information to a SQLite database and retrieve it later. Towards the end of the chapter, you learned how you can use JavaScript code using C# to execute method calls.
Up until this point, you have been building the TrackMyWalks
app with static walk trail information that has been hard-coded within the TrackMyWalks
app. However, in the real world, it is very rare that your app will depend purely on local static data, and you will need to source your information from a remote data source, typically using a RESTful API. In some cases, your app may even communicate with a third-party API, for example Facebook.
In this chapter, you'll learn how you can use Microsoft Azure App services to create your very first live, cloud-based backend HTTP web service to handle all the communication between the cloud and the app. You will also learn how to create a DataService
API that will allow the app to consume the API so that we can retrieve, store, and delete walk trail information from the cloud all within the TrackMyWalks
app.
This chapter will cover the following points:
TrackMyWalks
app within the Microsoft Azure portalHttpClient
and JSON.Net NuGet
packages to the solutionTrackMyWalks
base HTTP serviceTrackMyWalks
API data serviceTrackMyWalks
ViewModels
to use the API data serviceTrackMyWalks
app within the simulatorIn this section, we will look at the steps required to set up the TrackMyWalks
application within Microsoft Azure. Nearly all mobile applications that you will develop will require the ability to communicate with an API to store, retrieve, update, and delete information. This API can be an existing one that someone within your organization has already created, but sometimes you will need to create your own API for your application.
Microsoft Azure, or ("Azure" as it's best known for), is essentially a cloud-based platform that was created by Microsoft back in February 2010. Azure was designed for building, deploying, and managing several applications and their associated services, such as SaaS, PaaS, and IaaS.
Each of the Microsoft Azure specific associated services are explained in the following table:
Azure service |
Description |
SaaS |
Software as a Service provides software licensing and delivery models where software is licensed on a subscription basis and is centrally hosted. |
PaaS |
Platform as a Service provides customers with a platform to develop, run, and manage applications without the complexities of maintaining the infrastructure when developing and launching an app. |
IaaS |
Infrastructure as a Service provides virtualized computing resources over the Internet. |
One of the main benefits of using Microsoft Azure Mobile Apps is that they provide you with a very quick and easy way to get a fully functional backend service up and running within a matter of minutes. Before we can proceed with setting up and creating our TrackMyWalks
database within the cloud, you will need to have a Microsoft Azure account. If you don't already have one, you can create one for free at
https://azure.microsoft.com/en-us/pricing/free-trial/
.
Once you have created your Microsoft Azure account, you will need to log into the Microsoft Azure portal using your web browser. Let's look at how to do this, by performing the following steps:
TrackMyWalks
to use as the name for our app for the App name field.Now that we have successfully created our Mobile App within Microsoft Azure, our next step is to begin setting up the database that will allow our app to store walk entry information. Let's look at how we can achieve this with the following steps:
TrackMyWalks
SQL server database.Once you have created your TrackMyWalks
mobile app and SQL database within Microsoft Azure, by default, your database won't contain any database tables or data. Before we can start communicating with and consuming the API within our TrackMyWalks
app, we need to create a new table that will store our walk trail entries.
Let's look at the following steps to achieve this:
WalkEntries
to use as the name for our table for the Name field.
Before we can start making calls to our API and consuming this within our TrackMyWalks
app, we'll run a quick check to see if our API endpoint is working correctly. This is achieved by issuing a GET
HTTP request using the command line, or if you'd prefer, you can use a REST console client.
Last login: Sun Nov 6 10:48:41 on console GENIESOFT-MAC-Mini:~ stevendaniel$ curl https://trackmywalks.azurewebsites.net/tables/ walkentries --header "ZUMO-API-VERSION:2.0.0"
If you have set everything up correctly within the Microsoft Azure portal, you should receive back a 200
(Success) status code, along with an empty collection in the response body as follows:
Last login: Sun Nov 6 10:48:41 on console GENIESOFT-MAC-Mini:~ stevendaniel$ curl https://trackmywalks.azurewebsites.net/tables/ walkentries --header "ZUMO-API-VERSION:2.0.0" [] GENIESOFT-MAC-Mini:~ stevendaniel$
There are several REST console clients that exist for you to choose from, if you don't already have one installed. I tend to use Postman
for handling REST APIs, which you can download from
http://www.getpostman.com/.
Now that we have successfully created our TrackMyWalks
API and WalkEntries
data table within the service, we can begin making calls to our API and receiving those response messages directly back from the API. In the next section, we will begin to add the Json.Net
and HttpClient
.NET Framework libraries that will be responsible for handling the REST API requests to save and retrieve our walk entry details.
Now that you have set up and created the TrackMyWalks
database within the Microsoft Azure platform, our next step is to add the Json.Net NuGet
package to our TrackMyWalks
Portable Class Library solution. The Json.Net
package is a high-performance JSON framework for the .NET platform that allows you to serialize and deserialize any type of .NET object with help of the JSON serializer.
When we start to incorporate this framework within our TrackMyWalks
solution, we will have the ability of performing LINQ to JSON capabilities that will enable us to create, parse, query, and modify the JSON structure that we receive back from our Microsoft Azure TrackMyWalks
database table.
Let's look at how to add the Json.Net NuGet
package to our TrackMyWalks
Portable Class Library , by performing the following steps:
Packages
folder that is contained within the TrackMyWalks
Portable Class Library solution, and choose Add Packages... menu option, as shown in the following screenshot:
Json.Net
within the search dialog, and select the Json.Net option within the list, as shown in the following screenshot:
NuGet
package to the Packages
folder, contained within the TrackMyWalks
Portable Class Library solution.Now that you have added the Json.Net NuGet
package, our next step is to add the HttpClient framework to our TrackMyWalks
Portable Class Library, which we will be covering in the next section.
In the previous section, we added the Json.Net NuGet
package to our TrackMyWalks
solution. Our next step is to add the HTTP library to our TrackMyWalks
solution to enable it to communicate with an API over HTTP.
Since we are using both .NET and C# to build our Xamarin.Forms
application, we can leverage a library within the .NET Framework called System.Net.Http.HttpClient
. This HttpClient framework provides us with a mechanism of sending and receiving data using standard HTTP methods, such as GET
and POST
.
Let's look at how to add the HttpClient NuGet
package to our TrackMyWalks
Portable Class Library, by performing the following steps:
Packages
folder that is contained within the TrackMyWalks
Portable Class Library solution, and choose the Add Packages... menu option. If you can't remember how to do this, you can refer to the section entitled Adding the Json.Net NuGet
package to the TrackMyWalks
app located within this chapter.Http
within the search dialog, and select the System.Net.Http option, as shown in the following screenshot:
NuGet
package to the Packages
folder, contained within the TrackMyWalks
Portable Class Library solution.Now that you have added both the Json.Net
and System.Net.HttpNuGet
packages to our solution, we can begin utilizing these framework libraries as we progress throughout this chapter.
In this section, we will begin by updating the WalkEntries
data model to take advantage of our backend service calls, when we create these, and then the WalkDataService.cs
and WalkWebService.cs
files will communicate and interact with our Microsoft Azure TrackMyWalks
database to store, delete, and retrieve walk entry information.
Let's now start to modify and implement the code required for our WalkEntries
class model, by performing the following steps:
Ensure that the WalkEntries.cs
file is displayed within the code editor, and enter in the following highlighted code sections:
// // WalkEntries.cs // TrackMyWalks // // Created by Steven F. Daniel on 04/08/2016. // Copyright © 2016 GENIESOFT STUDIOS. All rights reserved. // using System; using Newtonsoft.Json; namespace TrackMyWalks.Models { public class WalkEntries { [JsonProperty("id")] public string Id { get; set; } public string Title { get; set; } public string Notes { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public double Kilometers { get; set; } public string Difficulty { get; set; } public double Distance { get; set; } public Uri ImageUrl { get; set; } } }
In the preceding code snippet, we have successfully modified the database model that will be used to store walk entry information within our Microsoft Azure database. You will notice that we have defined a [JsonProperty("id")]
item, as well as a string
property named Id
that will serve as a unique primary key for each record that we store within the database. We have also updated our ImageUrl
property to include the Uri
type that will be used to convert the URL entered within the walk entry page, so that it is stored correctly within the database.
If you are interested in finding out more information about the JsonProperty
and the Newtonsoft.Json
classes, please refer to the Json.NET documentation located at
http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_JsonProperty.htm
.
3.138.120.136