CHAPTER 1 ■ CREATE CLOUD TABLE STORAGE 23 Close SQL Analysis When creating a test cloud storage table, any active SQL query analysis running against the existing cloud storage table ne
Trang 1CHAPTER 1 ■ CREATE CLOUD TABLE STORAGE
23
Close SQL Analysis
When creating a test cloud storage table, any active SQL query analysis running against the existing
cloud storage table needs to be closed from SQL Management Studio to avoid the failure of data storage creation
Summary
As we have learned so far, creating cloud table storage is a straightforward job Microsoft claims that the Azure framework is a true developer-oriented framework All the hard work should have been
done and encapsulated in the SDK Your life as a NET developer is now a lot easier; what remains for you to do is nothing but derive your client classes from the base classes from the SDK and follow the right procedures to build your cloud applications However, I have not presented how to access cloud table storage yet That is the task we are going to pursue in the next chapter
Trang 3C H A P T E R 2
■ ■ ■
25
Access Cloud Table Storage
To access Azure table storage, we must use Azure web roles, since the worker role cannot connect to
or communicate with the outside world directly A worker role is usually used on batch jobs (see the
book's introduction) The Azure SDK provides a set of templates in a client service access library,
StorageClient.dll This chapter has three exercises In the first section of this chapter we will walk
through a process to access cloud storage step by step and see how to use these templates to insert and query data using the cloud table storages The second exercise focuses on deleting the records from a
cloud storage table The third exercise is an example of how to manipulate data in tables with a
relational structure The cloud storage we are going to use was created in Chapter 1 From these three
exercises you will learn the basic skills to deal with cloud table storage
Essentially all cloud tables are addressable using a URI The cloud table can be accessed by a web
request via HTTP The Azure SDK provides client-side components that implement all the base classes used to access Azure table storage These components can be found in the bin folder of the SDK You
then need to create classes derived from the base classes defined in the SDK
Accessing a Single Cloud Data Storage Table
First, let’s see how to access a single cloud data storage table Before we drill down into the details let’s create a new WebRole cloud service in Visual Studio by selecting New Project ➤ Visual C# ➤ Cloud
Service ➤ Web Cloud Service and name it CloudTableStorageService
■ Note The code for this example is in the exercise 2-1 bundle from the code download
Visual Studio will automatically generate two projects as a solution package One project is called
CloudTableStorageService, and another one is called CloudTableStorageService WebRole The first
project is a new type of Visual Studio project introduced by the Windows Azure framework
There are two files automatically generated in the CloudTableStorageService project by Visual
Studio One is ServiceConfiguration.cscf, a configuration XML file containing user account
information By default, all the URI addresses generated in this file point to the local development
environment with a default account authentication key called AccountSharedKey with a base-64
encoding token key value When a project is deployed to the remote cloud host in a Microsoft data
center, all the values in the configuration need to be changed to point to your account You will find out how to do this later in this book and get information in more detail about deploying an Azure project to a
Trang 4CHAPTER 2 ■ ACCESS CLOUD TABLE STORAGE
26
remote cloud environment from Chapter 9 Visual Studio also adds a reference to point to the second project The second project, CloudTableStorageService WebRole, is a regular ASP.NET project with a default web form file defined inside the project Renamed it AddressTable.aspx
Right-click on the second project we have just created, CloudTableStorageService WebRole, and insert three new folders into that project—CloudTableStorageDataContext, CloudTableStorageDataService, and CloudTableStorageDataEntity We are going to add and implement more classes in these three folders in the rest of this exercise
■ Note You can delete the second web role project and create your own regular web application type project and add it back by changing the reference to your own web application project By design there is only one web application project that can be referenced from an Azure storage service project
1 As Listing 2-1 shows, create a base class TableContext This class inherits
from TableStorageDataServiceContext This class is part of the Microsoft.ServiceHosting.Service.dll assembly from the Azure SDK
The constructor of the TableContext class takes StorageAccountInfo as a parameter The StorageAccountInfo class instance retrieves the configuration (DevelopmentStorage.exe.config, shown in Listing 2-2) from the SDK install directory and reads it at runtime There is no need to do any more work with this derived class All necessary jobs and hard tasks have been done from the base class This makes your life a lot easier since you do not need to know the details of how to hook up to the Azure framework runtime environment The only thing for you to do is to provide correct authentication information
Listing 2-1 A Cloud Data Object Class Must Inherit from the Base Class TableStorageDataServiceContext
abstract public class TableContext : TableStorageDataServiceContext
{
public string TableName { get; set; }
public TableContext(StorageAccountInfo accountInfo)
: base(accountInfo)
{
}
}
Listing 2-2 The Configuration File DevelopmentStorage.exe.config
<developmentStorageConfig>
<services>
<service name="Blob"
url="http://127.0.0.1:10000/"/>
<service name="Queue"
url="http://127.0.0.1:10001/"/>
Trang 5CHAPTER 2 ■ ACCESS CLOUD TABLE STORAGE
27
<service name="Table"
url="http://127.0.0.1:10002/"
dbServer="localhost\SQLExpress"/>
</services>
<accounts>
<account name="devstoreaccount1"
authKey="<AUTH KEY>"
isAdmin="false"
contactInfo=""/>
</accounts>
</developmentStorageConfig>
2 Create another class called DataTableService This class has two member
variables One is the type of the TableContext class we have just created,
and the another member variable is the StorageAccountInfo type This
class is also defined in the Microsoft.ServiceHosting.Service.dll
assembly of the Azure SDK In the body of the constructor of the
DataTableService class, add a line of code to instantiate the class
StorageAccountInfo and the retrieve the account configuration
information from the configuration file DevelopmentStorage.exe.config
as shown in Figure 2-1 Figure 2-1 shows the debug information at the
breakpoint set at the constructor of the DataTableService class at runtime
The configuration information retrieved by the framework should match the
configuration settings The implementation of the TableContext class is fairly
straightforward This class only implements a query interface to the cloud
table AddressTable, as Listing 2-1 and the class diagram in Figure 2-2 show
Figure 2-1 Account information is configured in DevelopmentStorage.exe.config and retrieved at runtime