共用方式為


Building Windows Azure Service Part2: Service Project

In this post, you will create a Windows Azure service project using Microsoft Visual Studio 2010.

To create the Windows Azure service project

  1. From the start menu, select All Programs and right-click Microsoft Visual Studio 2010.

  2. Choose Run as Administrator.If the User Account Control dialog appears, click Continue.

  3. From the File menu, click New Project.

  4. In the New Project dialog window expand the language of your choice (in this walkthrough, we are using C#) and then select Cloud.

  5. In the templates list, select Windows Azure Cloud Service.

  6. In the Name box, enter GuestBook.

  7. In the Location box, enter the location where to store the project.

  8. Click OK to create the project.

    image 

    Figure 2 Creating Windows Azure Service Project

  9. In the New Cloud Service Project dialog window, in the .NET Framework Roles panel select ASP.NET Web Role.

  10. Click the right arrow to add an instance of the selected role to the solution.

  11. The role shows in the Cloud Service Solution right panel.

  12. Click the pencil icon.

  13. Rename the role GuestBook_WebRole.

  14. Click OK to create the cloud service solution.

    image 

    Figure 3 Assigning Roles to the Service Project

  15. In Solution Explorer review the project structure.

    image 

    Figure 4 GuestBook Service Project Structure

As shown in the previous illustration, the solution contains two separate projects.

  • GuestBook project. It contains the following files and folder:

    • ServiceDefinition.csdef. This is the application definition file that specifies the application requirements for the Windows Azure fabric such as which roles are used, their trust level, the end points exposed by each role, the local storage requirements and the certificates used by the roles. It also defines specific application settings. The following code shows the content of the definition file.
     <?xml version="1.0" encoding="utf-8"?>
    <ServiceDefinition name="GuestBook" xmlns="https://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
      <WebRole name="GuestBook_WebRole">
        <InputEndpoints>
          <InputEndpoint name="HttpIn" protocol="http" port="80" />
        </InputEndpoints>
        <ConfigurationSettings>
          <Setting name="DiagnosticsConnectionString" />
          <Setting name="DataConnectionString" />
        </ConfigurationSettings>
      </WebRole>
      <WorkerRole name="GuestBook_WorkerRole">
        <ConfigurationSettings>
          <Setting name="DiagnosticsConnectionString" />
          <Setting name="DataConnectionString" />
        </ConfigurationSettings>
      </WorkerRole>
    </ServiceDefinition>
    

     

    • ServicesConfiguration.cscfg. This is the configuration file that specifies the number of instances for each role and assigns the values for the configuration settings as defined in the ServiceDefinition.csdef file. The following code shows the content of the configuration file.
     <?xml version="1.0"?>
    <ServiceConfiguration serviceName="GuestBook" xmlns="https://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
      <Role name="GuestBook_WebRole">
        <Instances count="1" />
        <ConfigurationSettings>
          <Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
          <Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
        </ConfigurationSettings>
      </Role>
      <Role name="GuestBook_WorkerRole">
        <Instances count="1" />
        <ConfigurationSettings>
          <Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
          <Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
        </ConfigurationSettings>
      </Role>
    </ServiceConfiguration>
    

    The separation between service definition and configuration enables you to update the settings of a deployed application by just uploading a new service configuration file.

    • Roles node. It enables you to configure what roles the cloud service includes that are web, worker or both. It also enables you to specify which project to associate with these roles.

    Adding and configuring roles in the Roles node will update the definition and configuration files.

  • GuestBook_WebRole project. This is a standard ASP.NET web application modified for the Windows Azure environment. It contains an additional class that provides the entry point for the web role and contains methods for initializing, starting and stopping the role.

For related topics, see the following posts.