私はWorker Roleを持っていますので、展開すると一度動作してから何もしない、ここに私のコードがあります。 この場所のみを変更しました。機能OnStartとOnStopはデフォルトです。Azure Workerの役割はちょうど開始時に働いています
public class WorkerRole : RoleEntryPoint
{
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);
public override void Run()
{
Thread.Sleep(10000);
Trace.TraceInformation("WorkerRole is running");
while(true)
{
try
{
this.RunAsync(this.cancellationTokenSource.Token).Wait();
}
finally
{
this.runCompleteEvent.Set();
}
}
}
public override bool OnStart()
{
ServicePointManager.DefaultConnectionLimit = 12;
bool result = base.OnStart();
Trace.TraceInformation("WorkerRole has been started");
return result;
}
public override void OnStop()
{
Trace.TraceInformation("WorkerRole is stopping");
this.cancellationTokenSource.Cancel();
this.runCompleteEvent.WaitOne();
base.OnStop();
Trace.TraceInformation("WorkerRole2 has stopped");
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
private CloudBlobContainer GetImageBlobContainer()
{
var storageAccount = CloudStorageAccount.Parse("myconnectionstring");
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("cadblob");
container.CreateIfNotExists();
container.SetPermissions(
new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
return container;
}
public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth/image.Width;
var ratioY = (double)maxHeight/image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
return newImage;
}
private async Task RunAsync(CancellationToken cancellationToken)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Myconnectionstring");
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("cadqueue");
CloudQueueMessage retrievedMessage = queue.GetMessage();
if (retrievedMessage.AsString != null || retrievedMessage.AsString != "")
{
string[] msg = retrievedMessage.AsString.Split(' ');
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("cadtable");
TableOperation retrieveOperation = TableOperation.Retrieve<ImageEntity>("Images", msg[0]);
TableResult retrievedResult = table.Execute(retrieveOperation);
var container = GetImageBlobContainer();
msg[2] = "mini_" + msg[2];
CloudBlockBlob blockBlob = container.GetBlockBlobReference(msg[2]);
blockBlob.Properties.ContentType = msg[1];
Image img;
var webClient = new WebClient();
byte[] imgBytes = webClient.DownloadData(((ImageEntity)retrievedResult.Result).Full_img);
using (var ms = new MemoryStream(imgBytes))
{
img = Image.FromStream(ms);
}
Image thumb = ScaleImage(img, 220, 160);
var fileBytes = imageToByteArray(thumb);
await blockBlob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length);
ImageEntity ent = (ImageEntity)retrievedResult.Result;
if (ent != null)
{
ent.Mini_img = "https://cadwebstorage.blob.core.windows.net/cadblob/" + msg[2];
TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(ent);
table.Execute(insertOrReplaceOperation);
}
else
{
Console.WriteLine("Entity could not be retrived.");
}
queue.DeleteMessage(retrievedMessage);
}
while (!cancellationToken.IsCancellationRequested)
{
Trace.TraceInformation("Working");
await Task.Delay(10000);
}
}
}
全体のクラスはここに投稿できますか? –
ええ、確かに、投稿を編集して明確にします。 – oceankit