Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

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

5.00/5 (1 vote)
15 Jan 2015CPOL5 min read 9.3K  
This article has the goal to show how to create a development and production environment for a .Net Backend from Azure Mobile Services.

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 Image 1

And

Image 2

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

Image 3

After it, click in Configuration Manager as following

Image 4

And then create a new configuration as following

Image 5

Image 6

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

Image 7

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

Image 8

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

Image 9

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

Image 10

Which the result will be

Image 11

Now is possible

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

XML
<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!)

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

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

XML
<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

Image 12

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

Image 13

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

Image 14

And then publish it!

Do the same process to Prod, as following

Image 15

And then publish it!

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

Image 16

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)