Donnerstag, 28. Januar 2010

Windows Azure Training Kit

Beschäftige mich seit mehreren Tagen mit dem Windows Azure Training Kit.
>hier besorgen

Das braucht man noch für Vista SP 1:
- Windows Azure Software Development Kit
- KB967631 - Update for Visual Studio 2008 SP1 Debugger
- Update: Das FastCGI-Modul

Bei IIS muss man dann noch über Systemstuerung->Programme-> Windows Funktionen ein-/auschalten die Funktionen ASP.NET und CGI in Internetinformationsdienste/WWW-Dienste/Anwendungsentwicklungsfeatures aktivieren.

Ggf. noch das Visual Studio Service Pack 1

Was einem dann auffällt, ist die seltsame Mischung der Sprachen C# / VB.Net (:-() bei dem Demoprojekt Guestbook. Habe deshalb mal den Worker - einer der Rollen innerhalb eines Cloud-Projekts - in C# umgeschrieben:


// Comment 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.IO;
using GuestBook_Data;
using System.Drawing;

namespace GuestBook_WorkerRole
{
    public class WorkerRole : RoleEntryPoint
    {

        private CloudQueue queue;
        private CloudBlobContainer container;

        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.TraceInformation("Listening for queue messages...");

            while (true)
            {

                try
                {
                    CloudQueueMessage msg = queue.GetMessage();
                    if (msg != null)
                    {
                        var messageParts = msg.AsString.Split(',');
                        var uri = messageParts[0];
                        var partitionKey = messageParts[1];
                        var rowkey = messageParts[2];
                        Trace.TraceInformation("Processing image in blob '{0}'.", uri);

                        CloudBlockBlob imageBlob = container.GetBlockBlobReference(uri);
                        MemoryStream image = new MemoryStream();
                        imageBlob.DownloadToStream(image);
                        image.Seek(0, SeekOrigin.Begin);

                        String thumbnailUri = String.Concat(Path.GetFileNameWithoutExtension(uri), "_thumb.jpg");
                        CloudBlockBlob thumbnailBlob = container.GetBlockBlobReference(thumbnailUri);
                        thumbnailBlob.UploadFromStream(CreateThumbnail(image));

                        GuestBookEntryDataSource ds = new GuestBookEntryDataSource();
                        ds.UpdateImageThumbnail(partitionKey, rowkey, thumbnailBlob.Uri.AbsoluteUri);

                        queue.DeleteMessage(msg);

                        Trace.TraceInformation("Generated thumbnail in blob '{0}'.", thumbnailBlob.Uri);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                catch (StorageClientException e)
                {
                    Trace.TraceError("Exception when processing queue item. Message: '{0}'", e.Message);
                    System.Threading.Thread.Sleep(5000);
                }

            }
        }

        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            DiagnosticMonitor.Start("DiagnosticsConnectionString");

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
            RoleEnvironment.Changing += RoleEnvironmentChanging;



            Microsoft.WindowsAzure.CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                {
                    configSetter(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName));
                });


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

            var blobStorage = storageAccount.CreateCloudBlobClient();
            container = blobStorage.GetContainerReference("guestbookpics");

            var queueStorage = storageAccount.CreateCloudQueueClient();
            queue = queueStorage.GetQueueReference("guestthumbs");

            Trace.TraceInformation("Creating container and queue...");

            var storageInitialized = false;
            do
            {
                try
                {
                    container.CreateIfNotExist();
                    var permissions = container.GetPermissions();
                    permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                    container.SetPermissions(permissions);

                    queue.CreateIfNotExist();
                    storageInitialized = true;
                }
                catch (StorageClientException e)
                {
                    if (e.ErrorCode == StorageErrorCode.TransportError)
                    {
                        Trace.TraceError("Storage services initialization failure. "
                          + "Check your storage account configuration settings. If unning locally, "
                          + "ensure that the Development Storage service is running. Message: '{0}'", e.Message);
                        System.Threading.Thread.Sleep(5000);
                    }
                    else
                        throw;
                }


            } while (!storageInitialized);


            return base.OnStart();
        }

        private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
        {
            // If a configuration setting is changing
            if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
            {
                // Set e.Cancel to true to restart this role instance
                e.Cancel = true;
            }
        }

        private Stream CreateThumbnail(Stream input)
        {
            Bitmap orig = new Bitmap(input);
            int width;
            int height;

            if (orig.Width > orig.Height)
            {
                width = 128;
                height = 128 * orig.Height / orig.Width;
            }
            else
            {
                height = 128;
                width = 128 * orig.Width / orig.Height;
            }
            Bitmap thumb = new Bitmap(width, height);

            using (Graphics graphic = Graphics.FromImage(thumb))
            {
                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphic.DrawImage(orig, 0, 0, width, height);
                MemoryStream ms = new MemoryStream();
                thumb.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Seek(0, SeekOrigin.Begin);
                return ms;
            }
        }
    }
}

Keine Kommentare:

Kommentar veröffentlichen