我有一個工作者角色,所以當我部署它時,它工作一次,然後什麼都不做,這是我的代碼,我該如何解決這個問題? 我只改變了這個地方,函數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