1 out of 3 rated this helpful - Rate this topic

How to: Configure the Roles for a Windows Azure Application with Visual Studio

A Windows Azure application can have one role or many roles. For each role you need to define how that role is set up and also configure how that role runs. This information for your application is stored in the following files:

  • ServiceDefinition.csdef

    The service definition file defines the runtime settings for your application including what roles are required, endpoints, and virtual machine size. None of the data stored in this file can be changed when your role is running.

  • ServiceConfiguration.cscfg

    The service configuration file configures how many instances of a role are run and the values of the settings defined for a role. The data stored in this file can be changed while your role is running.

To be able to store different values for these settings for how your role runs, you can have multiple service configurations. You can use a different service configuration for each deployment environment. For example, you can set your storage account connection string to use the local Windows Azure storage emulator in a local service configuration and create another service configuration to use the Windows Azure storage in the cloud.

When you create a new Windows Azure application in Microsoft Visual Studio, two service configurations are created by default. These configurations are added to your Windows Azure project. The configurations are named:

  • ServiceConfiguration.Cloud.cscfg

  • ServiceConfiguration.Local.cscfg

Configure a Windows Azure Application

You can configure a Windows Azure application from Solution Explorer in Visual Studio, as shown in the following illustration.

VS_Solution_Explorer_Roles_Properties

To Configure a Windows Azure Application

  1. To configure each role in your Windows Azure project from Solution Explorer, open the shortcut menu for the role in the Windows Azure project and then choose Properties.

    A page with the name of the role is displayed in the Visual Studio editor. The page displays the fields for the Configuration tab.

  2. In the Service Configuration list, choose the name of the service configuration that you want to edit.

    If you want to make changes to all of the service configurations for this role, you can choose All Configurations.

    ImportantImportant
    If you choose a specific service configuration, some properties are disabled because they can only be set for all configurations. To edit these properties, you must choose All Configurations.

You can now choose a tab to update any enabled properties on that view. For information about specific changes that you might want to make, see the following procedures:

Change the Number of Instances

To improve the performance of your application, you can change the number of instances of a role that are running, based on the number of users or the load expected for a particular role. A separate virtual machine is created for each instance of a role when the application runs in Windows Azure. This will affect the billing for the deployment of this application. For more information about billing, see Windows Azure Billing Basics.

To change the number of instances for a role

  1. Choose the Configuration tab.

  2. In the Service Configuration list, choose the service configuration that you want to update.

    noteNote
    You can set the instance count for a specific service configuration or for all service configurations.

  3. In the Instance count text box, type the number of instances that you want to start for this role.

    noteNote
    Each instance is run on a separate virtual machine when you publish your application to Windows Azure.

  4. Choose the Save icon in the toolbar to save these changes to the service configuration file.

Manage Connection Strings for Storage Accounts

You can add, remove or modify connection strings for your service configurations. You might want different connection strings for different service configurations. For example, you might want a local connection string for a local service configuration that has a value of UseDevelopmentStorage=true. You might also want to configure a cloud service configuration that uses a storage account in Windows Azure.

CautionCaution
When you enter the Windows Azure storage account key information for a storage account connection string, this information is stored locally in the service configuration file. However, this information is currently not stored as encrypted text.

By using a different value for each service configuration, you do not have to use different connection strings in your application or modify your code when you publish your application to Windows Azure. You can use the same name for the connection string in your code and the value will be different, based on the service configuration that you select when you build your application or when you publish it.

To manage connection strings for storage accounts

  1. Choose the Settings tab.

  2. In the Service Configuration list, choose the service configuration that you want to update.

    noteNote
    You can update connection strings for a specific service configuration, but if you need to add or delete a connection string you must select All Configurations.

  3. To add a connection string, choose the Add Setting button.

    A new entry is added to the list.

  4. In the Name text box, type the name that you want to use for the connection string.

  5. In the Type drop-down list, choose Connection String.

  6. To change the value for the connection string, choose the ellipsis button.

    The Storage Account Connection String dialog box is displayed.

  7. To use a local connection string, choose Use the Windows Azure storage emulator radio button and then choose the OK button.

  8. To use a storage account for the local connection string, choose Enter storage account details radio button. For information about how to create a storage account and how to enter the details for the storage account in the Storage Account Connection String dialog box, see Create a Storage Account and Configure Your Application to Use Services Provided by the Storage Account.

  9. To delete a connection string, select the connection string and then choose the Remove Setting button.

  10. Choose the Save icon in the toolbar to save these changes to the service configuration file.

  11. To access the connection string in the service configuration file, you must get the value of the configuration setting. The following code shows an example where blob storage is created and data uploaded using a connection string MyConnectionString from the service configuration file when a user chooses Button1 from the default.aspx page in the web role for an Azure application:

    Add the following using statements to Default.aspx.cs:

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;
    using Microsoft.WindowsAzure.ServiceRuntime;
    
    

    Add the following code to the Button1_Click method, after you add Button1 to the default.aspx page. This code uses GetConfigurationSettingValue to get the value from the service configuration file for the connection string. Then a blob is created in the storage account that is referenced in the connection string MyConnectionString and finally the program adds text to the blob.

    
    protected void Button1_Click(object sender, EventArgs e)
    {
        // Setup the connection to Windows Azure Storage
        var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("MyConnectionString"));
        var blobClient = storageAccount.CreateCloudBlobClient();
        // Get and create the container
        var blobContainer = blobClient.GetContainerReference("quicklap");
        blobContainer.CreateIfNotExist();
        // upload a text blob
        var blob = blobContainer.GetBlobReference(Guid.NewGuid().ToString());
        blob.UploadText("Hello Windows Azure");
      
    }
    
    

Add custom settings to use in your Windows Azure application

Custom settings in the service configuration file let you add a name and value for a string for a specific service configuration. You might choose to use this setting to configure a feature in your application by reading the value of the setting and using this value to control the logic in your code. You can change these service configuration values without having to rebuild your service package or when your application is running. Your code can check for notifications of when a setting changes. For more information about this, see How to Read Configuration Settings and Handle Changed Settings.

You can add, remove or modify custom settings for your service configurations. You might want different values for these strings for different service configurations.

By using a different value for each service configuration, you do not have to use different strings in your application or modify your code when you publish your application to Windows Azure. You can use the same name for the string in your code and the value will be different, based on the service configuration that you select when you build your application or when you publish it.

To add custom settings to use in your Windows Azure application

  1. Choose the Settings tab.

  2. In the Service Configuration list, choose the service configuration that you want to update.

    noteNote
    You can update strings for a specific service configuration, but if you need to add or delete a string, you must select All Configurations.

  3. To add a string, choose the Add Setting button.

    A new entry is added to the list.

  4. In the Name text box, type the name that you want to use for the string.

  5. In the Type drop-down list, choose String.

  6. To add or change the value for the string, in the Value text box type the new value.

  7. To delete a string, select the string and then choose the Remove Setting button.

  8. Choose the Save icon in the toolbar to save these changes to the service configuration file.

  9. To access the string in the service configuration file, you must get the value of the configuration setting.

    You need to make sure that the following using statements are already added to Default.aspx.cs just as you did in the previous procedure:

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;
    using Microsoft.WindowsAzure.ServiceRuntime;
    
    

    Add the following code to the Button1_Click method to access this string in the same way that you access a connection string. Your code can then perform some specific code based on the value of the settings string for the service configuration file that is used.

    
    var settingValue = RoleEnvironment.GetConfigurationSettingValue("MySetting"); 
    if (settingValue == “ThisValue”)
    { 
    // Perform these lines of code
    }
    
    

Manage local storage for each role instance

You can add local file system storage for each instance of a role. You can store local data here that does not need to be accessed by other roles. Any data that you do not need to save into table, blob, or SQL Azure storage can be stored in here. For example, you could use this local storage to cache data that might need to be used again. This stored data can’t be accessed by other instances of a role. For more information about local storage resources, see How to Configure Local Storage Resources.

Local storage settings apply to all service configurations. You can only add, remove, or modify local storage for all service configurations.

To manage local storage for each role instance

  1. Choose the Local Storage tab.

  2. In the Service Configuration list, choose All Configurations.

  3. To add a local storage entry, choose the Add Local Storage button.

    A new entry is added to the list.

  4. In the Name text box, type the name that you want to use for this local storage.

  5. In the Size text box, type the size in MB that you need for this local storage.

  6. To remove the data in this local storage when the virtual machine for this role is recycled, select the Clean on role recycle check box.

  7. To edit an existing local storage entry, choose the row that you need to update. Then you can edit the fields, as described in the previous steps.

  8. To delete a local storage entry, choose the storage entry in the list and then choose the Remove Local Storage button.

  9. To save these changes to the service configuration files, choose the Save icon in the toolbar.

  10. To access the local storage that you have added in the service configuration file, you must get the value of the local resource configuration setting. Use the following lines of code to access this value and create a file called MyStorageTest.txt and write a line of test data into that file. You can add this code into the button click method that you used in the previous procedures:

    You need to make sure that the following using statements are added to Default.aspx.cs:

    using System.IO;
    using System.Text;
    
    

    Add the following code to the Button1_Click method. This creates the file in the local storage and writes test data into that file.

    
    // Retrieve an object that points to the local storage resource
    LocalResource localResource = RoleEnvironment.GetLocalResource("LocalStorage1");
    
    //Define the file name and path
    string[] paths = { localResource.RootPath, "MyStorageTest.txt" };
    String filePath = Path.Combine(paths);
    
    using (FileStream writeStream = File.Create(filePath))
    {
          Byte[] textToWrite = new UTF8Encoding(true).GetBytes("Testing Web role storage");
          writeStream.Write(textToWrite, 0, textToWrite.Length);
    }
    
    
  11. (Optional) To view this file that you created when you run your application locally, use the following steps:

    1. In the notification area, open the shortcut menu for the Windows Azure icon and choose Show Compute Emulator UI.

      The Windows Azure Compute Emulator dialog box is displayed.

    2. On the menu bar, choose Tools, Open local store.

      A Windows Explorer window is displayed.

    3. On the menu bar, type MyStorageTest.txt into the Search text box and then press Enter to start the search.

      The file is displayed in the search results.

    4. To view the contents of the file, open the shortcut menu for the file and choose Open.

Collect diagnostics

You can collect diagnostics data for your Windows Azure application. This data is added to a storage account. You might want different connection strings for different service configurations. For example, you might want a local storage account for a local service configuration that has a value of UseDevelopmentStorage=true. You might also want to configure a cloud service configuration that uses a storage account in Windows Azure. For more information about Windows Azure diagnostics, see Collecting Logging Data by Using Windows Azure Diagnostics.

noteNote
The local service configuration is already configured to use local resources. If you use the cloud service configuration to publish your Windows Azure application, the connection string that you specify when you publish is also used for the diagnostics connection string unless you have specified a connection string. If you package your application using Visual Studio, the connection string in the service configuration is not changed.

To collect diagnostics

  1. Choose the Configuration tab.

  2. In the Service Configuration list, choose the service configuration that you want to update or choose All Configurations.

    noteNote
    You can update the storage account for a specific service configuration, but if you want to enable or disable diagnostics you must choose All Configurations.

  3. To enable diagnostics, select the Enable Diagnostics check box.

  4. To change the value for the storage account, choose the ellipsis button.

    The Storage Account Connection String dialog box is displayed.

  5. To use a local connection string, choose Use the Windows Azure storage emulator radio button and then choose the OK button.

  6. To use a storage account for the local connection string, choose the Enter storage account details radio button.

    For more information about how to create a storage account and how to enter the details for the storage account in the Storage Account Connection String dialog box, see Create a Storage Account.

  7. Type the name of your storage account in Account name.

    noteNote
    The name of your storage account is displayed in the Storage Accounts view in the Management Portal. To copy this name, highlight the name in the Properties window of this view and press Ctrl+C. To paste the name into Visual Studio, choose Account name and then press Ctrl+V.

  8. You can copy or type your primary key in Account key. This key can be copied from the Management Portal. To copy this key, following these steps from the Storage Accounts view in the Management Portal:

    1. Select the storage account that you want to use for your application.

    2. The primary key for this storage account is displayed in the Properties view. Choose the View button located next to the key.

      The View Storage Access Keys dialog box is displayed.

    3. To copy the access key, choose the Copy to clipboard button. You can now paste this key into the Account key field.

  9. To use the storage account that you provide, as the connection string for diagnostics when you publish your application to Windows Azure, select the Use publish storage account as connection string when you publish to Windows Azure check box.

  10. Choose the Save icon in the toolbar to save these changes to the service configuration file.

Set the Trust Level for Your Windows Azure Application

Permissions that are available to a role deployed to Windows Azure depend on the trust level with which the role is published. The two levels of trust available are Full trust and Windows Azure partial trust. By default, a role's trust level is set to Full trust. These trust levels must be the same for all service configurations.

Full trust is necessary to enable native code execution.

For more information about trust level, see Windows Azure Partial Trust Policy.

noteNote
By default, the trust level for a role is set to Full trust. Setting the trust level to Windows Azure partial trust adds restrictions to loading and usage of the Windows Azure client libraries.

ImportantImportant
Cloud services on Windows Azure run in a 64-bit operating system in the cloud. The use of native code execution features such as PInvoke could require migration to 64-bit in order to work correctly.

To set the trust level

  1. Choose the Configuration tab.

  2. In the Service Configuration list, choose All Configurations.

  3. To enable full access to Windows Azure native libraries and resources and to access diagnostics, choose the Full trust radio button. This is the default setting.

  4. To restrict access for your Windows Azure application to native libraries and resources, choose the Windows Azure partial trust radio button.

  5. Choose the Save icon in the toolbar to save these changes to the service configuration file.

Change the Size of the Virtual Machine Used for Each Role

You can set the virtual machine size for each role. You can only set this size for all service configurations. If you select a smaller machine size, then less CPU cores, memory and local disk storage is allocated. The allocated bandwidth is also smaller. For more information about these sizes and the resources allocated, see How to Configure Virtual Machine Sizes.

The resources required for each virtual machine in Windows Azure affects the cost of running your application in Windows Azure. For more information about Windows Azure Billing, see Windows Azure Billing Basics.

To change the size of the virtual machine

  1. Choose the Configuration tab.

  2. In the Service Configuration list, choose All Configurations.

  3. To select the size for the virtual machine for this role, choose the appropriate size from the VM size list.

  4. Choose the Save icon in the toolbar to save these changes to the service configuration file.

Connect Local Computers to Windows Azure Roles

If you want to set up an IP connection between a role and other computers that are running applications outside Windows Azure, you can associate a Windows Azure Connect token with a role. You must have activated the token from the Management Portal. For more information about Windows Azure Connect, see Using Windows Azure Connect to Create Virtual Networks.

You must activate or deactivate Windows Azure Connect for all service configurations, but you can use a different activation token for different service configurations.

To connect local computers to Windows Azure roles

  1. Choose the Virtual Network tab.

  2. In the Service Configuration list, choose All Configurations.

  3. To activate Windows Azure Connect for this role, select the Activate Windows Azure Connect check box.

  4. To add or change a token for all configurations or a specific configuration, choose the configuration from the Service Configuration list and then paste the activation token from the Management Portal into the text box.

  5. Choose the Save icon in the toolbar to save these changes to the service configuration files.

Manage Endpoints and Certificates for Your Roles

You configure networking endpoints by specifying the protocol, the port number, and, for HTTPS, the SSL certificate information. Releases before June 2012 support HTTP, HTTPS, and TCP. The June 2012 release supports those protocols and UDP. You can’t use UDP for input endpoints in the compute emulator. You can use that protocol only for internal endpoints.

To improve the security of your Windows Azure application, you can create endpoints that use the HTTPS protocol. For example, if you have an application that is used by customers to purchase orders, you want to make sure that their information is secure by using SSL.

You can also add endpoints that can be used internally or externally. External endpoints are called input endpoints. An input endpoint allows another access point to users to your application. If you have a WCF service, you might want to expose an internal endpoint for a web role to use to access this service.

ImportantImportant
You can only update endpoints for all service configurations.

If you add HTTPS endpoints, you need to use an SSL certificate. To do this you can associate certificates with your role for all service configurations and use these for your endpoints.

ImportantImportant
These certificates are not packaged with your service. You must upload your certificates separately to Windows Azure through the Windows Azure Platform Management portal

For more information about how to upload your management certificates to Windows Azure, see How to Add a Management Certificate to a Windows Azure Subscription.

Any management certificates that you associate with your service configurations apply only when your application runs in Windows Azure. When your application runs in the local development environment, a standard certificate that is managed by the Windows Azure compute emulator is used.

To add a certificate to a role

  1. Choose the Certificates tab.

  2. In the Service Configuration list, choose All Configurations.

    noteNote
    To add or remove certificates, you must select All Configurations. You can update the name and the thumbprint for a specific service configuration if it is required.

  3. To add a certificate for this role, choose the Add Certificate button.

    A new entry is added to the list.

  4. In the Name text box, type the name for the certificate.

  5. In the Store Location list, choose the location for the certificate that you want to add.

  6. In the Store Name list, choose the store that you want to use to select the certificate.

  7. To add the certificate, choose the ellipsis button.

    The Windows Security dialog box is displayed.

  8. Choose the certificate that you want to use from the list and then choose the OK button.

    noteNote
    When you add a certificate from the certificate store, any intermediate certificates are added automatically to the configuration settings for you. These intermediate certificates must also be uploaded to Windows Azure in order to correctly configure your service for SSL.

  9. To delete a certificate, choose the certificate and then choose the Remove Certificate button.

  10. Choose the Save icon in the toolbar to save these changes to the service configuration files.

To manage endpoints for a role

  1. Choose the Endpoints tab.

  2. In the Service Configuration list, choose All Configurations.

  3. To add an endpoint, choose the Add Endpoint button.

    A new entry is added to the list.

  4. In the Name text box, type the name that you want to use for this endpoint.

  5. Choose the type of endpoint that you need from the Type list.

  6. Choose the protocol for the endpoint that you need from the Protocol list.

  7. If it is an input endpoint, in the Public Port text box type the public port to use.

  8. In the Private Port text box type the private port to use.

  9. If the endpoint requires the https protocol, in the SSL Certificate Name list choose a certificate to use.

    noteNote
    This list shows the certificates that you have added for this role in the Certificates tab.

  10. Choose the Save icon in the toolbar to save these changes to the service configuration files.

See Also

Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.
facebook page visit twitter rss feed newsletter