0

好的,我意识到 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 内容,但随后我需要执行整个连接操作才能将其删除.. 这不对.. 可以吗?

4

2 回答 2

3

BlobAttribute 也可用于 .NET SDK 类型。在您的情况下,您可以绑定到 CloudBlockBlob,然后不需要连接字符串。

CloudBlockBlob blobReference = binder.Bind<CloudBlockBlob>(new BlobAttribute(Email.blobEmailOutboxContainerAsPath+blobname));
blobReference.DeleteIfExists();

作为旁注,您还可以绑定到 CloudStorageAccount。如果您的 Web 作业有 CloudStorageAccount 参数(不需要属性),它将被神奇地绑定。

于 2014-07-16T18:09:32.940 回答
0

有了这个答案,我也成功了。以下是补充说明

就我而言,我必须通过使用和删除来更改 BlobAttribute

//use block blob
//Must be "FileAccess.Read"
var fileAttr = new BlobAttribute(anyblobname, FileAccess.Read);
using(var stream = binder.Bind<Stream>(fileAttr)){
  ...
}

//delete block blob
//Must be "FileAccess.ReadWrite"
var blobAttr = new BlobAttribute(anyblobname, FileAccess.ReadWrite);
var blockBlob = binder.Bind<CloudBlockBlob>(blobAttr);
blockBlob.DeleteIfExists();
于 2017-03-17T02:44:03.140 回答