Azure Mobile Services: How to creating a Dev and Prod environments

Scope

This article has the goal to show how to create a development and production environment for a .Net Backend from Azure Mobile Services.

Introduction

According the MSDN Documentation,

Microsoft Azure Mobile Services is an Azure service offering designed to make it easy to create highly-functional mobile apps using Azure. Mobile Services brings together a set of Azure services that enable backend capabilities for your apps. Mobile Services provides the following backend capabilities in Azure to support your apps:

  • Client libraries support mobile app development on various devices, including Windows 8, Windows Phone 8, iPhone, and iPad.
  • Simple provisioning and management of tables for storing app data. 
  • Integration with notification services to deliver push notifications to your app.
  • Integration with well-known identity providers for authentication.
  • Precise control for authorizing access to tables.
  • Supports scripts to inject business logic into data access operations.
  • Integration with other cloud services.
  • Supports the ability to scale a mobile service instance.
  • Service monitoring and logging.

This way, Azure Mobile Services will increase the mobile application development and the fact it is available for the main mobile platform is a plus.

In some scenarios, more specific while new features are provided to the mobile applications that is published in the store and used by users, different environment from the Azure Mobile Service can be required, because each version can change or even break the last backend version from Azure Mobile Service and is important to test and keep the production running. Azure Mobile Services allow to test the services in localhost, but not provides a way to have different environments and to create different environments is required to have one Azure Mobile Service for each environment and then create configuration for each one.

Description

To create a development and production environment for a .Net Backend – Azure Mobile Services is required to create a Dev and Prod Backend and then is possible to create the Dev and Prod profiles based in transform files.

Let’s see it in more detail.

Creating the Azure Mobile Services

In Azure Portal, create two .Net Backend – Azure Mobile Service, let’s calling it as following

  • For the development environment:
    • MyAMSDev – the Azure Mobile Service name
    • MyAMSDev_db – the database name
  • For the production environment:
    • MyAMSProd – the Azure Mobile Service name
    • MyAMSProd_db – the database name

In Azure Portal, the result will be azure portal

And

azure portal

Is possible to use the same database, but for it is necessary to use different schemas and if different databases are provided is easier for manage each environment (update or even delete). A Backend that uses Entity Framework migrations will recreate the service from the scrats and with it any time it can be delete, deployed and recreated.

To read more about the steps required to create an Azure Mobile Service, read the following article

How to create the MyMenuApp Azure Mobile Service.

Creating Transform File

In Visual Studio, select the solution and open the context menu as following

azure mobile service

After it, click in Configuration Manager as following

azure mobile service

And then create a new configuration as following

azure mobile service

azure mobile service

In this case, a Dev configuration is defined based in Debug configuration and to the Prod configuration should be used the Release configuration as following

azure mobile service

At this moment, is possible to define Debug, Dev, Prod and Release configuration

azure mobile service

For each configuration is possible to define the conditional compilation. For example, in Dev is possible to have

azure mobile service

In reality it mean, we can do

#if Dev

// for example can fill the database with fake data

#else

// in other environment different from Dev

#endif

To have advantages of this configurations using the Web.Config, let’s add the config transform as following

azure mobile service

Which the result will be

azure mobile service

Now is possible

– To set in the Web.Config, the connection string to the local database, used by the localhost

 <connectionStrings>
    <add name="MS_TableConnectionString1" connectionString="Data Source=LT114925;Initial Catalog=MyAMSLocal;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
   </connectionStrings>

– To set in the Web.Dev.Config, the connection string to the MyAMSDev_db database, used by the Dev environment (or even the localhost!)

      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

– To set in the Web.Prod.Config, the connection string to the MyAMSProd_db database, used by the Prod environment

   <connectionStrings>
      <add name="MS_TableConnectionString1"
        connectionString="<connection string from MyAMSProd_db>"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>

And in each deploy the respective configs will be used.

Let’s publish the service to the Azure.

Select the project and using the context menu click in Publish, as following

azure mobile service

Then connect to Azure Mobile Service, using the Azure Account, and select the MyAMSDev as following

azure mobile service

In Settings, set the configuration and the file publish options (only!)

azure mobile service

And then publish it!

Do the same process to Prod, as following

azure mobile service

And then publish it!

Is possible to see the Web Publish Activity that show the publish process

azure mobile service

At this moment, both environment are deployed and running and is not required to define or change each environment in future, only is needed to have attention each one is set before deploy.

Notes

    1. In this case was use the MS_TableConnectionString1 name for define the connection string and the default value MS_TableConnectionString, defined in Azure Portal, will be ignored.

    A developer can to set different connect string or even change the name, but needs to be defined with the same name in all environments.

    1. With the Visual Studio 2013 – update 4 is possible to change the database in Settings separator but in this case it will be ignored!

For more information about the transform file, read the following article

How to: Transform Web.config When Deploying a Web Application Project

Conclusion

In conclusion, creating different environments for a .Net Backend – Azure Mobile Service is an easy task that is a plus in the development, which allow to test new features before it go to production and it allow to make sure the tests done in localhost will keep working in the Azure Mobile Service without to affect the production environment.