1. Trang chủ
  2. » Công Nghệ Thông Tin

Introducing Windows Azure- P27 pdf

5 253 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 103 (paramters as BlobStorageFacade).CreateBlob(); } catch { } } } } Listing 3-18. The DeleteBlobStatus Class Derived from the BlobStorageAction Class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace AzureForDotNetDevelopers.LargeDataToBlob.CloudStorage.BlobStorage { using Microsoft.ServiceHosting.ServiceRuntime; using Microsoft.Samples.ServiceHosting.StorageClient; using CSharpBuildingBlocks; public class DeleteBlobStatus : BlobStorageActionStatus, ICommand { public DeleteBlobStatus(BlobContents blobContents, BlobProperties blobProperties, bool overwrite) : base(blobContents, blobProperties, overwrite) { } public void Execute() { try { if (! blobStorageFacade.BlobContainer .DoesBlobExist( blobStorageFacade.Properties.Name)) { percentComplete = 100.0f; } } catch { } } override protected void blobStorageWorkerThread(object paramters) { try { (paramters as BlobStorageFacade).DeleteBlob(); Thread.Sleep(2000); CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 104 } catch { } } } } The CSharpBuildingBlock.QueuedBackgroundWorker assembly contains a building block component called QueuedBackgroundWorkerComponent. This component can place a worker item into a queue and run from a background thread. There are three events that can be fired up from that component, DoWork, ProgressChanged, and RunWorkerCompleted as Figure 3-12 shows. The event handlers need to be registered either at design time via the properties dialog as in Figure 3-12 or programmatically as shown in Listing 3-19. Listing 3-20 shows the event handlers for DoWork, RunWorkerCompleted, and ProgressChanged. After accomplishing these steps to create or delete blob storage with a large amount of data the rest is relatively simple and straightforward. What is left for you is instantiating CreateBlobStatus of DeleteBlobStatus as an ICommand type and passing it to QueuedBackgroundWorkerItem. The job will run in the background without locking up the UI thread. The progress and task accomplished event will be reported to the client as we expected. The upload blob name can be automatically generated. If the size of the contents of blob is greater than 2 MB, a suffix large is attached to the end of the name. There is no particular constraint for the blob name as long as it meets the specification illustrated in Appendix A. To interrupt ongoing processing, either for creating or deleting, a user needs to set the flag of CancelAll from QueuedBackgroundWorkerItem by clicking on the Abort button. Figure 3-12. Event bindings if using the QueuedBackgroundWorkerComponent CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 105 The rest of this chapter, from Listing 3-19 to Listing 3-23, shows the implementation of a Windows form user interface, including the event handling, application initialization, and GUI updating. This part should be straightforward to any professional .NET developer, and we are not going to drill down to analysis in detail. You can download the source code and verify the results attached to the end of this chapter and potentially use this as an administration tool to transmit a large amount of data to blob storage as part of the infrastructure of a cloud application. Listing 3-19. Register Events for Background Worker Items from the Client UI Class Constructor public FormBlobAccess() { InitializeComponent(); this. backgroundWorkeComponent = new QueuedBackgroundWorkeComponent(); this._backgroundWorkeComponent.DoWork += new System.ComponentModel.DoWorkEventHandler (this._backgroundWorker_DoWork); this._backgroundWorkeComponent.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler( this._backgroundWorker_RunWorkerCompleted ); this._backgroundWorkeComponent.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler( this._backgroundWorkeComponent_ProgressChanged ); UpdateUI(); } Listing 3-20. Event Handler for DoWorker, RunWorkerCompleted, and ProgressChanged private void backgroundWorker DoWork(object sender, DoWorkEventArgs e) { if (e.Argument is CreateBlobStatus) { command = (CreateBlobStatus)e.Argument; if ( backgroundWorkeComponent. QueuedBackgroundWorker .IsCancellationPending( command)) { e.Cancel = true; } } else if (e.Argument is FibGeneratorState) { command = (FibGeneratorState)e.Argument; } if (null != command) { while (! backgroundWorkeComponent. QueuedBackgroundWorker CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 106 .IsCancellationPending( command) && command.PercentComplete < 100) { backgroundWorkeComponent. QueuedBackgroundWorker .ReportProgress( command.PercentComplete, command); command.Execute(); e.Result = command; Application.DoEvents(); } } } private void backgroundWorker RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (null != fileStream) { fileStream.Close(); toolStripProgressBar1.Visible = false; } lblTimeEnd.Text = DateTime.Now.ToString(); if ( command is CreateBlobStatus) { toolStripStatusLabel1.Text = "Upload blob success."; } else if ( command is DeleteBlobStatus) { toolStripStatusLabel1.Text = "Delete blob success."; } UpdateUI(); } private void backgroundWorkeComponent ProgressChanged(object sender, ProgressChangedEventArgs e) { // progressBar.Value = e.ProgressPercentage; Update(); Application.DoEvents(); } Listing 3-21. Instantiate an Instance of CreateBlobStatus and Pass It to QueuedBackgroundWorker to Create a Large Blob in the Background private void btnUpload Click(object sender, EventArgs e) { toolStripProgressBar1.Visible = true; toolStripStatusLabel1.Text = string.Empty; ; btnAbort.Enabled = true; CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 107 btnDelete.Enabled = false; lblTimeStart.Text = DateTime.Now.ToString(); lblTimeEnd.Text = string.Empty; lblTimeElapsed.Text = string.Empty; timer1.Enabled = true; if (null == azureStorage) { azureStorage = new AzureStorageFacade(); } fileStream = new FileStream( fileName.Text.ToString().Trim(), FileMode.Open, FileAccess.Read); if (txtBlobName.Text.ToString().Trim() == string.Empty) { btnGenerateBlobName Click(this, null); } string blobName = txtBlobName.Text.ToString().Trim(); BlobProperties properties = new BlobProperties(blobName); NameValueCollection metadata = new NameValueCollection(); properties.Metadata = metadata; properties.ContentType = "byte"; BlobContents blobContents = new BlobContents( fileStream); _command = new CreateBlobStatus(blobContents, properties, true) { CreateContentSize = _fileInfo.Length } ; _backgroundWorkeComponent._QueuedBackgroundWorker.RunWorkerAsync(_command); } Listing 3-22. Instatiate an Instance of DeleteBlobStatus and Pass It to QueuedBackgroundWorker to Delete a Large Blob from Background private void btnDelete Click(object sender, EventArgs e) { toolStripProgressBar1.Visible = true; toolStripStatusLabel1.Text = string.Empty; toolStripStatusLabel1.ForeColor = Color.Black; btnAbort.Enabled = true; lblTimeStart.Text = DateTime.Now.ToString(); lblTimeEnd.Text = string.Empty; lblTimeElapsed.Text = string.Empty; timer1.Enabled = true; . 105 The rest of this chapter, from Listing 3-19 to Listing 3-23, shows the implementation of a Windows form user interface, including the event handling, application initialization, and GUI

Ngày đăng: 05/07/2014, 01:20