我有一个辅助角色,所以当我部署它时,它只运行一次,然后什么都不做,这是我的代码,我该如何解决这个问题? 我只改变了这个地方,功能OnStart和OnStop是默认的。
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);
}
}
}
答案 0 :(得分:2)
由于此循环,图像缩放代码仅工作一次的原因
while (!cancellationToken.IsCancellationRequested)
{
Trace.TraceInformation("Working");
await Task.Delay(10000);
}
在private async Task RunAsync(CancellationToken cancellationToken)
内。除非您停止工作器实例,否则永远不会终止。
要修复它,您可以将图像处理代码放在循环中,如下所示
private async Task RunAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
ScaleImage();
await Task.Delay(10000);
}
}
private void ScaleImage()
{
Trace.TraceInformation("Scaling Image");
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(true)
中的循环Run()
。 Run()
可以像这样简单
public override void Run()
{
try
{
RunAsync(cancellationTokenSource.Token).Wait();
}
finally
{
runCompleteEvent.Set();
}
}