Web.config settings are not accessible to WebRoles. Instead, the WebRole configuration is accessible through the RoleEnvironment. Here is a simple pattern for defining and accessing those settings.
In Azure, service configuration is associated with each role. The role’s configuration is dynamic and can be modified after deployment without restarting the service. The configuration information specified in ServiceConfiguration.cscfg can be accessed within the role’s assembly using:
RoleEnvironment.GetConfigurationSettingValue("MySetting1")
An example configuration file is provided below; note that each role has access to its own configuration settings only. In addition to the settings, each role can be deployed as a continuous array of instances, as specified by the <Instances /> element.
Azure WebRole ServiceConfiguration.cscfg.
<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="ManageableServicesExplorer"
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"
osFamily="3" osVersion="*" schemaVersion="2012-10.1.8">
<Role name="Role1">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="MySetting1" value="..." />
</ConfigurationSettings>
</Role>
<Role name="Role2">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="MySetting2" value="..." />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
Using VisualStudio, it is possible to edit the initial settings used upon WebRole deployment as follows:
Step 1: Expand the ‘roles folder’ under the Service project.
Step 2: Select the specific role, and right-click on ‘properties’.
Step 3: Select the ‘settings’ tab in the properties form.
Step 4: Add/Edit strings, or connection strings, as needed; the ‘name’ will be used in the code to identify the settings and retrieve its value.
To deploy the web-role follow the following steps:
Step 1: Select the Service project.
Step 2: Right-click on the service project and select ‘package’ to generate both the package and setting files; Visual Studio will open the file location of the output generated.
Step 4: Open the Azure portal service to which we need to deploy the WebRole.
step 5: Select ‘new deployment’ or ‘update’ as appropriate.
Step 6: Select the package and configuration file from the location provided at the end of step 2.
Step 7: Select the deployment options.
Step 8: Select ‘create’ or ‘update’.
Subsequently, after deployment, the Azure portal user-interface enables changing the settings, as follows:
Step 1: select the service to be configured.
Step 2: Select ‘configure’ at the top.
Step 3: Edit the settings as appropriate and save changes.
That’s it.
Leave a comment