Tải bản đầy đủ (.pdf) (7 trang)

Lập trình .net 4.0 và visual studio 2010 part 64 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (455.85 KB, 7 trang )

CHAPTER 16  WINDOWS AZURE

429
Local Storage
LocalStorage is an area to which you can temporarily save files and it can be useful for caching,
uploading, and serialization. Warning—you should not save anything here you want to keep since local
storage will be cleared if your application restarts.
To use LocalStorage, simply add a new entry to ServiceDefinition.csdef to define the storage area:

<LocalStorage name="MyStorage"/>

Once you have defined the storage you can now use the RoleEnvironment.GetLocalResource()
method to return a LocalResource object that allows you to utilize the file. The following example shows
how to save a file to local storage:

LocalResource resource = RoleEnvironment.GetLocalResource("MyStorage");
string Path = resource.RootPath + "messages.txt";
string FileContents = "Hello Azure";
System.IO.File.WriteAllText(Path, FileContents);

If you want to see items that have been saved in the development fabric’s local storage with the
previous code, then you can right-click on the web role and select Open local storage option and browse
to Directory/MyStorage.
Worker Roles
A worker role is Azure's version of a Windows service. Worker roles are used for continual or long-
running tasks, such as processing data held in an Azure queue (we will look at Azure queues shortly).
Worker roles cannot be accessed directly like a web role or ASP.NET page, but they do allow the creation
of HTTP-based endpoints for inter-role communication. Please see the Azure samples November
training kit ( />53b7b77edf78&displaylang=en), which contains a thumbnail image generator example.
In many Azure projects you will want to use both web and worker roles. To add a web or worker role
to an existing project, just right-click on the ~/Roles/ directory and then select to add a new worker role.


You may remember you also had the option to add a role when creating a project.
Let’s take a quick look at worker roles.
1. Right-click on the Roles directory and select AddNew Worker Role Project.
2. Call it Chapter16.WorkerRole.
3. Open WorkerRole.cs if it is not already open, and you should see code similar to the following
(shortened to save space). Note how a worker role at its most basic level is little more than a big
loop for you to put your code inside.
public override void Run()
{
Trace.WriteLine("WorkerRole1 entry point called", "Information");

while (true)
{
Thread.Sleep(10000);
Trace.WriteLine("Working", "Information");
}
}
CHAPTER 16  WINDOWS AZURE

430

public override bool OnStart()
{
ServicePointManager.DefaultConnectionLimit = 12;
DiagnosticMonitor.Start("DiagnosticsConnectionString");

RoleEnvironment.Changing += RoleEnvironmentChanging;
return base.OnStart();
}
}

Storage in Azure
For storing data in Windows Azure there are three main options:
• Azure Storage
• SQL Azure
• Other external storage mechanism accessible over HTTP
So what's the difference?
Azure storage is very fast and intended for storing files or data with a simple structure, and it is also
cheaper than its SQL counterpart. In contrast, SQL Azure is better suited to working with complex data
relationships and should be an easier option for application migration but is slower and more expensive.
SQL Azure is built on top of SQL Server but has a few important limitations, most notably a 10gb size
limit. SQL Azure also has a reduced set of functionality to normal SQL Server (although if you are using
only the basic/standard features of SQL Server, then your application will probably run fine on SQL
Azure). Note that initially SQL Azure (formally SQL Data Services) was similar to Azure table storage, but
due to customer feedback, it was changed to a more traditional SQL Server model.
The differences between the two services are summarised here:
• Azure Storage:
• More scalable than SQL Azure
• Stores Blobs, Queues, and Entities (a type of .NET objects)
• Cheaper than SQL Azure
• Does not use SQL Server (the development version does, though)
• Is not relational and doesn't use SQL
• Access is provided by the REST API
• SQL Azure
• SQL Server you know and love, offering an easier migration path for existing
applications
• Supports complex relational queries
• More expensive than Azure Storage
• Access is similar to standard SQL Server apart from using an Azure-specific
connection string
CHAPTER 16  WINDOWS AZURE


431
Before you jump to automatically using SQL Azure you may want to consider whether a traditional
relational database is scalable for very high traffic applications and whether you would be better served
using Azure Storage.
Azure Storage
Azure Storage holds three different types of data:
• Blobs - for files or large amounts of textual data
• Queues - messages retrieved in a first-in, first-out manner
• Tables - hold objects (called entities in Azure terminology) and bear little
resemblance to traditional storage mechanisms
Azure storage can be accessed by any application that can send an HTTP request, so don't think that
you are confined to using this service with just .NET applications. Azure storage can also be tested
locally by using the development storage. To access the development storage control panel, right-click
on the Windows Azure blue flag and select the show development fabric UI option.
The Development Storage management screen should then appear, showing the end points each of
the storage service is running at (Figure 16-17):

Figure 16-17. Development Storage UI
You can see that Azure Storage is divided into three different services of type: Blob, Queue, and
Table. The management screen shows each service’s current status and end points. Tables differ in that
they can be subdivided into containers.
Working with Azure Storage
To work with Azure storage there are two options:
• Make a request to the REST API directly
• Utilize the Windows Azure API, which makes REST requests behind the scenes
So you can see that ultimately you will be using the REST API or er the REST API.
Azure API or REST Requests?
The Azure APIs will be more than suitable for most applications, but for integration purposes or where
performance is paramount you may want to use the REST API directly, as it will give you full control over

CHAPTER 16  WINDOWS AZURE

432
your requests. However, before you rush off to develop your own REST API, here is a word of warning—
don’t underestimate the amount of work involved. Producing a class with enough functionality to work
with a single type of Azure storage data will mean creating many different methods and can be quite
boring, fiddly work.
Let's REST for a Minute
REST stands for Representational State Transfer and is a style of architecture introduced by a guy named
Roy Fielding (one of the main authors of HTTP). You can read about what Roy proposed at http://
www.ics.uci.edu/~fielding/pubs/dissertation/top.htm.
Applications implementing Roy’s proposed architecture are sometimes described as RESTful. I
don’t want to get into a debate about what exactly constitutes a RESTful system (some people that
probably need to get out a bit more feel scarily passionate about this) but the important points to note
are
• Everything is abstracted into a resource that is accessible by a unique address.
• REST applications don’t maintain state between requests.
These two characteristics might not seem like a big deal, but are essential for cloud-based
applications since they allow us to:
• Easily scale applications by taking advantage of features such as caching and load
balancing. There is no difference at an HTTP level between a request to Azure storage
and a web page request.
• Write inter-platform applications that integrate easily.
Azure Storage Names
Everything in Azure has to be accessible using HTTP, so Azure has a number of rules regarding naming
of objects that must be adhered to (basically anything that would form a valid URL address):
• Names must start with a letter or number.
• Names can only contain letters, numbers, and dashes.
• Every dash character must be preceded and followed by a letter.
• All letters must be lowercase.

• Names must be 3–63 characters in length.
Blobs (Binary Large Object)
Blobs are for storing binary data such as images, documents, and large strings. There are two types of
blobs in Windows Azure, block and page blobs. Block blobs are refined for streaming operations while
page blobs are used to write to a series of bytes. A block blob can be up to 200gb in size and is uploaded
in 64mb increments. Should your blob exceed 64mb then it will be split into individual
blocks
, which are
then reassembled. Page blobs can be up to 1 TB in size.
CHAPTER 16  WINDOWS AZURE

433
Blob Example
We will create a program to add, delete, and display blobs. Our application will allow the user to upload
images with the FileUpload control, which will then store them as a Blob. We will then bind the stored
Blobs to a DataList to check we have actually uploaded something.
1. Open Visual Studio and create a new Windows Azure Cloud Service called Chapter16.BlobTest
and add a web role called Chapter16.BlobTestWebRole.
2. Open Default.aspx and add the following code inside the form tag:
<asp:FileUpload ID="uploadFile" runat="server" /> <asp:Button ID="cmdUpload"
runat="server"
Text="Upload" />

<br /><br />

<asp:repeater ID="images" runat="server">
<ItemTemplate>
<asp:Image ID="image" runat="server" ImageUrl='<%# Eval("Url") %>' />
</ItemTemplate>
</asp:repeater>

3. Open Default.aspx.cs.
4. Add the following using statements:
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
5. Add the following code. Here, when the user uploads an image, an instance of the BlobClient is
created. The BlobClient then checks if a container called pictures exists and creates one if not.
Next we create a permission object to allow everyone to view our uploaded image before saving
the image. We then call the bindImages() method to display our uploaded images:
protected void Page_Load(object sender, EventArgs e)
{
this.cmdUpload.Click += new EventHandler(cmdUpload_Click);
bindImages();
}

void cmdUpload_Click(object sender, EventArgs e)
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
// Provide the configSetter with the initial value
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});

var storageAccount =
CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

CHAPTER 16  WINDOWS AZURE

434
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer blobContainer = blobClient.GetContainerReference("pictures");

blobContainer.CreateIfNotExist();

var permissions = blobContainer.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
blobContainer.SetPermissions(permissions);

blobContainer.GetBlockBlobReference(
Guid.NewGuid().ToString()).UploadFromStream(uploadFile.FileContent
);

bindImages();
}


public void bindImages()
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
// Provide the configSetter with the initial value
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});


var storageAccount =
CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobStorage.GetContainerReference("pictures");


blobContainer.CreateIfNotExist();

images.DataSource = from blob in blobContainer.ListBlobs()
select new { Url = blob.Uri };
images.DataBind();
}

6. The last step is that we need to tell Azure how to access the storage. Open
ServiceDefinition.csdef and add the following inside the ConfigurationSettings block:
<Setting name="DataConnectionString" />
7. Add the following settings in the ServiceConfiguration.cscfg configuration block:
<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
8. Press F5 to run your project.
9. Click Browse, select a JPG or GIF image, and click Upload and you should then see your picture
displayed like in Figure 16-18.
CHAPTER 16  WINDOWS AZURE

435

Figure 16-18. Example blob application
If you right-click on the image to examine its URL, notice how the URL is made up of a number of
properties we defined in our ServiceConfiguration: AccountName, pictures container, and the GUID we
used for the ID (this URL is made up of IP:PORT/account/container/blobID) (e.g., http://
127.0.0.1:10000/devstoreaccount1/pictures/4d5eee66-162e-4fb1-afcb-197f08384007).

Accessing REST API Directly
Now that we have worked with the StorageClient, however, I think that it is useful to understand what is
happening behind the scenes. In our previous example we created a container to store our images,
called pictures. We will now create an application to list all the containers in our local Azure Storage by

constructing the raw HTTP request.
How Do We Work with the REST API?
To interface with the Azure Storage REST API, we will construct a request using the WebRequest classes.
We need to do the following:

×