Exportera (0) Skriv ut
Visa allt
EN
Det här innehållet finns inte tillgängligt på ditt språk men här finns den engelska versionen,

How to: Configure the Roles for an Azure Cloud Service with Visual Studio

Updated: August 11, 2014

An Azure cloud service can have one or more worker or web roles. For each role you need to define how that role is set up and also configure how that role runs. To learn more about roles in cloud services, see the video Introduction to Windows Azure Cloud Services. The information for your cloud service is stored in the following files:

  • ServiceDefinition.csdef

    The service definition file defines the runtime settings for your cloud service 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 Azure storage emulator in a local service configuration and create another service configuration to use the Azure storage in the cloud.

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

  • ServiceConfiguration.Cloud.cscfg

  • ServiceConfiguration.Local.cscfg

You can configure an Azure cloud service from Solution Explorer in Visual Studio, as shown in the following illustration.

VS_Solution_Explorer_Roles_Properties

  1. To configure each role in your Azure project from Solution Explorer, open the shortcut menu for the role in the 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:

To improve the performance of your cloud service, 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 cloud service runs in Azure. This will affect the billing for the deployment of this cloud service. For more information about billing, see Azure Billing Basics.

  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 cloud service to Azure.

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

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 Azure.

CautionCaution
When you enter the 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 cloud service or modify your code when you publish your cloud service to 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 cloud service or when you publish it.

  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 Create Storage Connection String dialog box is displayed.

  7. To use the local storage account emulator, choose Microsoft Azure storage emulator radio button and then choose the OK button.

  8. To use a storage account in Azure, choose the Your subscription radio button, and select the desired storage account.

  9. To use custom credentials, choose the Manually entered credentials radio button. Enter the storage account name, and either the primary or second key. For information about how to create a storage account and how to enter the details for the storage account in the Create Storage Connection String dialog box, see Create a Storage Account and Configure Your Application to Use Services Provided by the Storage Account.

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

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

  12. 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 cloud service:

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

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.ServiceRuntime;
    
    

    Open Default.aspx.cs in design view, and add a button from the toolbox. Add the following code to the Button1_Click method. 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 Azure Storage
        var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("MyConnectionString"));
        var blobClient = storageAccount.CreateCloudBlobClient();
        // Get and create the container
        var blobContainer = blobClient.GetContainerReference("quicklap");
        blobContainer.CreateIfNotExists();
        // upload a text blob
        var blob = blobContainer.GetBlockBlobReference(Guid.NewGuid().ToString());
        blob.UploadText("Hello Azure");
      
    }
    
    

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 cloud service 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 cloud service is running. Your code can check for notifications of when a setting changes. See RoleEnvironment.Changing EventHow 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 cloud service or modify your code when you publish your cloud service to 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 cloud service or when you publish it.

  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.Storage;
    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
    }
    
    

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 Database 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 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.

  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 cloud service locally, use the following steps:

    1. Run the web role and click on Button1 to make sure that the code inside Button1_Click gets called.

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

      The Azure Compute Emulator dialog box is displayed.

    3. Select the web role.

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

      A Windows Explorer window is displayed.

    5. 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.

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

You can collect diagnostics data for your Azure cloud service. 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 Azure. For more information about Azure diagnostics, see Collect Logging Data by Using Azure Diagnostics.

noteNote
The local service configuration is already configured to use local resources. If you use the cloud service configuration to publish your Azure cloud service, 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 cloud service using Visual Studio, the connection string in the service configuration is not changed.

  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 Create Storage Connection String dialog box is displayed.

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

  6. To use a storage account associated with your Azure subscription, choose the Your subscription option.

  7. To use a storage account for the local connection string, choose the Manually entered credentials option.

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

  8. Choose the storage account you want to use in Account name.

  9. If you are manually entering your storage account credentials, 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 cloud service.

    2. Choose the Manage Access Keys button located at the bottom of the screen.

      The Manage 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.

  10. To use the storage account that you provide, as the connection string for diagnostics (and caching) when you publish your cloud service to Azure, select the Update development storage connection strings for Diagnostics and Caching with Windows Azure storage account credentials when publishing to Windows Azure check box.

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

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 Configure Sizes for Cloud Services.

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

  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.

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 Azure cloud service, you can create endpoints that use the HTTPS protocol. For example, if you have a cloud service 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 cloud service. 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 Azure through the Azure Platform Management portal

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

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

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

  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

Visa:
© 2014 Microsoft