public static async Task <bool> SendMessageToAzureServiceBusQueueAsync([ActivityTrigger] CloudBlobItem uploadedcloudBlob, ILogger log, ExecutionContext executionContext) { log.LogInformation($"Received event data with an uploaded cloud blob {uploadedcloudBlob.Name} with format {uploadedcloudBlob.FileType}."); //Config settings for Azure Service Bus var azureServiceBusConfig = new ConfigurationBuilder() .SetBasePath(executionContext.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var serviceBusConnection = azureServiceBusConfig["AzureServiceBusConnectionString"]; var serviceBusQueue = azureServiceBusConfig["ServiceBusQueueName"]; try { if (uploadedcloudBlob != null) { log.LogInformation($"Composing message to be sent to the queue"); var composedMessage = $"A blob image {uploadedcloudBlob.Name} was uploaded to Azure Service Bus Queue azdurablefunctioncloudqueue. </br> " + $"Blob Type: {uploadedcloudBlob.FileType} </br> " + $"Blob URL: {uploadedcloudBlob.BlobUrl} </br> " + $"Message sent via Azure Durable Functions App made by Jonah"; await using (ServiceBusClient client = new ServiceBusClient(serviceBusConnection)) { //Create sender ServiceBusSender sender = client.CreateSender(serviceBusQueue); //Create message ServiceBusMessage message = new ServiceBusMessage(composedMessage); //Send Message to ServiceBus Queue await sender.SendMessageAsync(message); log.LogInformation($"Sent a message to Service Bus Queue: {serviceBusQueue}"); return(true); } } else { return(false); } } catch (Exception ex) { log.LogInformation($"Something went wrong sending the message to the queue : {serviceBusQueue}. Exception {ex.InnerException}"); throw; } }
public static async Task <bool> SendEmailNotificationToAdmin([ActivityTrigger] CloudBlobItem uploadedBlob, ILogger log, ExecutionContext executionContext) { log.LogInformation($"BLOB already saved to queue."); try { //Config settings for Azure Service Bus var sendGridAPIConfig = new ConfigurationBuilder() .SetBasePath(executionContext.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var apiKey = sendGridAPIConfig["SendGridAPIKey"]; var adminEmail = sendGridAPIConfig["Admin_Email"]; var adminName = sendGridAPIConfig["Admin_Name"]; var client = new SendGridClient(apiKey); var from = new EmailAddress(adminEmail, adminName); List <EmailAddress> recipients = new List <EmailAddress> { new EmailAddress("*****@*****.**", "Jonah @JonahAndersson Tech"), new EmailAddress("*****@*****.**", "Jonah @Forefront") }; var subject = "New BLOB Uploaded on Azure Service Bus Queue "; var htmlContent = @"<p> A new cloud blob file added to Azure Service Bus queue. BLOB Url: </a>" + uploadedBlob.BlobUrl + "</a><br> Message from Jonahs app. </p>"; var displayRecipients = false; // set this to true if you want recipients to see each others mail id var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, recipients, subject, "", htmlContent, displayRecipients); var isEmailSent = await client.SendEmailAsync(msg); if (isEmailSent.IsSuccessStatusCode) { return(true); } else { return(false); } } catch (Exception ex) { //Error handling here // Console.WriteLine($"Receiving Service Bus Queue Message failed and email not sent: {ex.InnerException}"); log.LogError($"Receiving Service Bus Queue Message failed and email not sent: {ex.InnerException}"); throw; } }
public static async Task HttpBlobStart( [BlobTrigger("samples-workitems/{name}", Connection = "StorageConnectionString")] CloudBlockBlob myCloudBlob, string name, ILogger log, [DurableClient] IDurableOrchestrationClient starter) { try { log.LogInformation($"Started orchestration trigged by BLOB trigger. A blob item with name = '{name}'"); log.LogInformation($"BLOB Name {myCloudBlob.Name}"); // Function input comes from the request content. if (myCloudBlob != null) { var newUploadedBlobItem = new CloudBlobItem { Name = myCloudBlob.Name, BlobUrl = myCloudBlob.Uri.AbsoluteUri.ToString(), Metadata = (Dictionary <string, string>)myCloudBlob.Metadata, FileType = myCloudBlob.BlobType.ToString(), Size = myCloudBlob.Name.Length.ToString(), ETag = myCloudBlob.Properties.ETag.ToString() }; // Used if an object passed needs to be serialized // myCloudBlob.SerializeObjectToBlobAsync(newUploadedBlobItem).Wait(); var instanceId = await starter.StartNewAsync("AzureStorageNotifier_Orchestrator", newUploadedBlobItem); log.LogInformation($"Started orchestration with ID = '{instanceId}'."); } } catch (Exception) { //Errorhandling throw; } }