好的,我意识到 webjobs sdk 仍处于测试阶段,但它非常酷。尽管在获取我绑定的实际对象方面,我在 IBinder 上有点挣扎。这可能很明显,所以如果是,请原谅我......
我正在处理要通过网络作业发送的电子邮件。他们被放入队列并触发事件。此代码有效.. 但我不禁想到我可以访问生成的 blob,如果成功则删除它,或者如果不成功则移动它,更容易。
这是代码:
public static void ProcessEmailBlob([QueueTrigger(Email.queueEmailsToSend) ] string blobname, IBinder binder)
{
TextReader inputfile = binder.Bind<TextReader>(new BlobAttribute(Email.blobEmailOutboxContainerAsPath+blobname));
string input = inputfile.ReadToEnd();
string connection = ConfigurationManager.ConnectionStrings["AzureJobsStorage"].ConnectionString;
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connection);
//Get create connect to the outbox blob
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(Email.blobEmailOutboxContainer);
CloudBlockBlob emailin = container.GetBlockBlobReference(blobname);
MailMessage smptmail = new MailMessage();
//ought to be able to JSONise this??
//smptmail = JsonConvert.DeserializeObject<MailMessage>(input);
smptmail = XmlMailMessage.MakeSMPTMessage(input);
bool success = Email.Send(smptmail);
if (success && emailin.Exists()) //If sending the email succeeded
{
emailin.Delete();
}
else
{ //The email failed or the blob doesn't exist which is also odd and bad
if (emailin.Exists())
{ //Then the file is ok.. store it in the Failed Email
CloudBlobContainer failedcontainer = blobClient.GetContainerReference(Email.blobEmailFailedContainer);
failedcontainer.CreateIfNotExists();
CloudBlockBlob failedmailblob = failedcontainer.GetBlockBlobReference(blobname); // use the same name just a different container
failedmailblob.StartCopyFromBlob(emailin);
}
else
{
//log something here
}
}
}
如您所见,我可以使用 binder.Bind 块获取 blob 内容,但随后我需要执行整个连接操作才能将其删除.. 这不对.. 可以吗?