Azure: From your PC to the cloud

In this post I want to write some tipps how to deploy your project to the cloud in a way that allows you to have a local test configuration and a production configuration.

FAQ: My code runs in local AppFabric, but it does not when I deploy it to Azure
I had the same problem with my project until I discovered the following thing:

  1. VS 2010 will only publish external DLLs,that are referenced by your Azure Role Project where the “Copy Local” Property is set to TRUE. This is especially dangerous when you use e.g. the EntityFramework that reside within the GAC and that is used by your DataAccess Layer. You will have to first reference the EntityFramework.dll with your Azure Role Project. And second, set the Property “Copy Local” to TRUE.

FAQ: How can I have a separate test and release configuration
This was long time not clear too me, but here’s the answer:

  1. Forget about your “web.config” file in order to store Application Settings. It’s true, that you can use it, but if you use it, you will loose the ability to change configuration on the fly without deploying the Azure Role again.
  2. It’s better to store the Application Settings within the “ServiceConfiguration.cscfg”. It’s more or less the same like using the AppSettings in your web.config. You can access these settings using:
       RoleEnvironment.GetConfigurationSettingValue("YOUR_SETTING");
  3. Store your development settings within that file.
  4. Create a second “ServiceConfiguration.cscfg” file and store it into a independent Directory named e.g. “Release Config”
  5. It’s better not to publish your Project directly with Visual Studio to the Cloud, but to publish it to a local Directory. That is because within that local directory you can replace the configuration with your “ServiceConfiguration.cscfg” file from the “Release Config” directory.
  6. After that you can upload your project within the Azure Management Portal.
  7. Tipp! Now you can change your configuration online in your Azure Management Portal, without re-deploying your Project to Azure.

FAQ: How to change connection string of your Entity Framework in Release deployment
The Entity Framework stores the connection string within the web.config!, which is (see above) not wanted.

  1. We have to teach Entity Framework not to use the connection from the web.config. Therefore it’s a good way to place a static constructor in the Entity Framework Context class and set a new DefaultConnectionFactory.
            
            static MySyncContext()
            {
                Database.DefaultConnectionFactory = new AzureConnectionFactory();
                Database.SetInitializer(new CreateDatabaseIfNotExists());
            }
    
  2. The AzureConnectionFactory will now use the connection string from our WebRole Configuration. Here’s the implementation:

            
        public class AzureConnectionFactory : IDbConnectionFactory
        {
            public DbConnection CreateConnection(string nameOrConnectionString)
            {
                var connectionString = RoleEnvironment.GetConfigurationSettingValue("DatabaseConnection");
                var sqlConnectionFactory = new SqlConnectionFactory();
                return sqlConnectionFactory.CreateConnection(connectionString);
            }
        }
    
  3. As you can see, the connection is used from file “ServiceConfiguration.cscfg” and setting “DatabaseConnection”.

If I find some other interessting topics, I’m going to append them to the post.
Cheers
– Gerhard

kick it on DotNetKicks.com

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 106 other followers

%d bloggers like this: