Skip to main content

Mobile Services from Windows Azure - Data Store (part 2)

Part 1
When we need a backed to store data for a mobile application Windows Azure Mobile Services can be a great solution.
Let’s imagine that we need to develop a mobile application that need to store the GPS location of our client. For this purpose we can develop a complicated solution, with a backed that exposes this functionality as services. Also we will need an authentication mechanism that will add another layer of complexity.
All this functionalities are already supported by Windows Azure Mobile Services. In this blog post we will see how we can configure.
First step is to configure our Windows Azure account to have Mobile Services active. In this moment Mobile Services is in the preview, because of this before creating a Mobile Services you will need to sign up to this preview. From the new command of the portal you will find a link to the sign up page for the preview. The activation time period is very short. Each Mobile Services has a unique name, which will identify our service. At this step you will notify that a database needs to be attached to this service. You can use an existing one, or you can create a new one. This database will be used to store data.
After creating our own Mobile Service we will need to create a new table in the data section. Only the name of the table needs to be set. The columns will be automatically updated when you will insert entities in it.
Each new table that is created has some special permission that can be set. Each CRUD operation (Insert, Update, Delete, Read) can have a custom permissions set. We have 4 permissions available:
  • Anybody with the application key – anybody that has our application key can execute the command over the table
  • Everyone – anybody with our Mobile Services credetinals can execute the command over the table (even if the request don’t come from our application)
  • Only authenticate users – only users that were already authenticated in the application can execute the command over our table
  • Only scripts and admin – only scripts and administrators can execute the command over our table
In our case we will add a table named GPSLocation where the permissions will be set to “Only authenticate users”.
Next step is to create the entity that needs to be stored. In this moment Mobile Services can be used from iOS, Windows Phone and Windows Store (Windows 8) applications. We will use Windows Store in this example.
[DataContract]
public class Location
{
     int Id { get; set;}

     [DataMember]
     string Lat { get; set;}

     [DataMember]
     string Long { get; set;}
}
You notified that I decorated the class with DataContract. All entities that are saved need to be decorated with this attribute. This attributed is used when the entity is send to the Mobile Services and need to be serialized. Each property of our entity need to be a value type.
The Id property needs to be added to all items that are saved using Mobile Services. Each entity will be unique identified by this id. The auto-incrementation of this field will be made automatically by the system. Also we don’t need to add the “DataMember” attribute on this property. If we would need to references another entity that we will need to store the Id of the entity.
Mobile Services offer the build-in the mechanism that is used to consume data. The only thing that we need to have is the application url and application key. The application key is very important, because based on this people will be able to access your application.
You can use the same application key for an iOS and Windows Store application without any kind of problem. If your application key is compromised, a new one can be regenerated from the portal. In that moment you will have to make an update to your application with our new application key.
MobileServiceClient mobileService = new MobileServiceClient( [url], [applicationKey] );
This instance will be used to communicate with Mobile Services. All the request are made using HTTP/S and JSON. Yep, data are serialized in JSON format not in XML.
In the next post we will see how we can authenticate users to be able to add their GPS location. Remember, we set the rights to “Only authenticate users”. Because of this we need to be authenticating to make any kind of operations on our table. We need this because we don’t want to share locations between users.
Part 3

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(...

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...