From b92641aa7380caf47464016658d488b574040b4c Mon Sep 17 00:00:00 2001 From: jotrick Date: Fri, 20 Dec 2019 09:01:13 -0800 Subject: [PATCH] Changed how the configuration is built in order to not hit the multiple handles error (#139) --- .../CompanyCommunicatorDataFunction.cs | 23 +++++------ .../CompanyCommunicatorSendFunction.cs | 38 +++++++++---------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/Source/Microsoft.Teams.Apps.CompanyCommunicator.Send.Func/CompanyCommunicatorDataFunction.cs b/Source/Microsoft.Teams.Apps.CompanyCommunicator.Send.Func/CompanyCommunicatorDataFunction.cs index f3ba9a27e..6f03a627f 100644 --- a/Source/Microsoft.Teams.Apps.CompanyCommunicator.Send.Func/CompanyCommunicatorDataFunction.cs +++ b/Source/Microsoft.Teams.Apps.CompanyCommunicator.Send.Func/CompanyCommunicatorDataFunction.cs @@ -31,6 +31,8 @@ public class CompanyCommunicatorDataFunction private static SendingNotificationDataRepository sendingNotificationDataRepository = null; + private static IConfiguration configuration = null; + /// /// Azure Function App triggered by messages from a Service Bus queue /// Used for aggregating results for a sent notification. @@ -52,17 +54,16 @@ public async Task Run( ILogger log, ExecutionContext context) { - IConfiguration configuration = new ConfigurationBuilder() - .SetBasePath(context.FunctionAppDirectory) - .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables() - .Build(); + CompanyCommunicatorDataFunction.configuration = CompanyCommunicatorDataFunction.configuration ?? + new ConfigurationBuilder() + .AddEnvironmentVariables() + .Build(); // Simply initialize the variable for certain build environments and versions var maxMinutesOfRetryingDataFunction = 0; // If parsing fails, out variable is set to 0, so need to set the default - if (!int.TryParse(configuration["MaxMinutesOfRetryingDataFunction"], out maxMinutesOfRetryingDataFunction)) + if (!int.TryParse(CompanyCommunicatorDataFunction.configuration["MaxMinutesOfRetryingDataFunction"], out maxMinutesOfRetryingDataFunction)) { maxMinutesOfRetryingDataFunction = 1440; } @@ -70,13 +71,13 @@ public async Task Run( var messageContent = JsonConvert.DeserializeObject(myQueueItem); CompanyCommunicatorDataFunction.sentNotificationDataRepository = CompanyCommunicatorDataFunction.sentNotificationDataRepository - ?? new SentNotificationDataRepository(configuration, isFromAzureFunction: true); + ?? new SentNotificationDataRepository(CompanyCommunicatorDataFunction.configuration, isFromAzureFunction: true); CompanyCommunicatorDataFunction.notificationDataRepository = CompanyCommunicatorDataFunction.notificationDataRepository - ?? this.CreateNotificationRepository(configuration); + ?? this.CreateNotificationRepository(CompanyCommunicatorDataFunction.configuration); CompanyCommunicatorDataFunction.sendingNotificationDataRepository = CompanyCommunicatorDataFunction.sendingNotificationDataRepository - ?? new SendingNotificationDataRepository(configuration, isFromAzureFunction: true); + ?? new SendingNotificationDataRepository(CompanyCommunicatorDataFunction.configuration, isFromAzureFunction: true); var sentNotificationDataEntities = await CompanyCommunicatorDataFunction.sentNotificationDataRepository.GetAllAsync( messageContent.NotificationId); @@ -89,7 +90,7 @@ public async Task Run( return; } - await this.SendTriggerToDataFunction(configuration, messageContent); + await this.SendTriggerToDataFunction(CompanyCommunicatorDataFunction.configuration, messageContent); return; } @@ -151,7 +152,7 @@ public async Task Run( } else { - await this.SendTriggerToDataFunction(configuration, messageContent); + await this.SendTriggerToDataFunction(CompanyCommunicatorDataFunction.configuration, messageContent); } var operation = TableOperation.InsertOrMerge(notificationDataEntityUpdate); diff --git a/Source/Microsoft.Teams.Apps.CompanyCommunicator.Send.Func/CompanyCommunicatorSendFunction.cs b/Source/Microsoft.Teams.Apps.CompanyCommunicator.Send.Func/CompanyCommunicatorSendFunction.cs index 2b115f5ab..18f9a561d 100644 --- a/Source/Microsoft.Teams.Apps.CompanyCommunicator.Send.Func/CompanyCommunicatorSendFunction.cs +++ b/Source/Microsoft.Teams.Apps.CompanyCommunicator.Send.Func/CompanyCommunicatorSendFunction.cs @@ -49,6 +49,8 @@ public class CompanyCommunicatorSendFunction private static MessageSender sendQueueServiceBusMessageSender = null; + private static IConfiguration configuration = null; + /// /// Azure Function App triggered by messages from a Service Bus queue /// Used for sending messages from the bot. @@ -72,11 +74,10 @@ public async Task Run( { log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}"); - IConfiguration configuration = new ConfigurationBuilder() - .SetBasePath(context.FunctionAppDirectory) - .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables() - .Build(); + CompanyCommunicatorSendFunction.configuration = CompanyCommunicatorSendFunction.configuration ?? + new ConfigurationBuilder() + .AddEnvironmentVariables() + .Build(); var messageContent = JsonConvert.DeserializeObject(myQueueItem); @@ -88,7 +89,7 @@ public async Task Run( var maxNumberOfAttempts = 0; // If parsing fails, out variable is set to 0, so need to set the default - if (!int.TryParse(configuration["MaxNumberOfAttempts"], out maxNumberOfAttempts)) + if (!int.TryParse(CompanyCommunicatorSendFunction.configuration["MaxNumberOfAttempts"], out maxNumberOfAttempts)) { maxNumberOfAttempts = 1; } @@ -97,26 +98,26 @@ public async Task Run( ?? new HttpClient(); CompanyCommunicatorSendFunction.userDataRepository = CompanyCommunicatorSendFunction.userDataRepository - ?? new UserDataRepository(configuration, isFromAzureFunction: true); + ?? new UserDataRepository(CompanyCommunicatorSendFunction.configuration, isFromAzureFunction: true); CompanyCommunicatorSendFunction.sendingNotificationDataRepository = CompanyCommunicatorSendFunction.sendingNotificationDataRepository - ?? new SendingNotificationDataRepository(configuration, isFromAzureFunction: true); + ?? new SendingNotificationDataRepository(CompanyCommunicatorSendFunction.configuration, isFromAzureFunction: true); CompanyCommunicatorSendFunction.globalSendingNotificationDataRepository = CompanyCommunicatorSendFunction.globalSendingNotificationDataRepository - ?? new GlobalSendingNotificationDataRepository(configuration, isFromAzureFunction: true); + ?? new GlobalSendingNotificationDataRepository(CompanyCommunicatorSendFunction.configuration, isFromAzureFunction: true); CompanyCommunicatorSendFunction.sentNotificationDataRepository = CompanyCommunicatorSendFunction.sentNotificationDataRepository - ?? new SentNotificationDataRepository(configuration, isFromAzureFunction: true); + ?? new SentNotificationDataRepository(CompanyCommunicatorSendFunction.configuration, isFromAzureFunction: true); if (CompanyCommunicatorSendFunction.botAccessToken == null || CompanyCommunicatorSendFunction.botAccessTokenExpiration == null || DateTime.UtcNow > CompanyCommunicatorSendFunction.botAccessTokenExpiration) { - await this.FetchTokenAsync(configuration, CompanyCommunicatorSendFunction.httpClient); + await this.FetchTokenAsync(CompanyCommunicatorSendFunction.configuration, CompanyCommunicatorSendFunction.httpClient); } CompanyCommunicatorSendFunction.sendQueueServiceBusMessageSender = CompanyCommunicatorSendFunction.sendQueueServiceBusMessageSender - ?? new MessageSender(configuration["ServiceBusConnection"], CompanyCommunicatorSendFunction.SendQueueName); + ?? new MessageSender(CompanyCommunicatorSendFunction.configuration["ServiceBusConnection"], CompanyCommunicatorSendFunction.SendQueueName); var getActiveNotificationEntityTask = CompanyCommunicatorSendFunction.sendingNotificationDataRepository.GetAsync( PartitionKeyNames.NotificationDataTable.SendingNotificationsPartition, @@ -151,7 +152,7 @@ public async Task Run( if (globalSendingNotificationDataEntity?.SendRetryDelayTime != null && DateTime.UtcNow < globalSendingNotificationDataEntity.SendRetryDelayTime) { - await this.SendDelayedRetryOfMessageToSendFunction(configuration, messageContent); + await this.SendDelayedRetryOfMessageToSendFunction(CompanyCommunicatorSendFunction.configuration, messageContent); return; } @@ -184,7 +185,7 @@ public async Task Run( "Bearer", CompanyCommunicatorSendFunction.botAccessToken); - var payloadString = "{\"bot\": { \"id\": \"28:" + configuration["MicrosoftAppId"] + "\"},\"isGroup\": false, \"tenantId\": \"" + incomingUserDataEntity.TenantId + "\", \"members\": [{\"id\": \"" + incomingUserDataEntity.UserId + "\"}]}"; + var payloadString = "{\"bot\": { \"id\": \"28:" + CompanyCommunicatorSendFunction.configuration["MicrosoftAppId"] + "\"},\"isGroup\": false, \"tenantId\": \"" + incomingUserDataEntity.TenantId + "\", \"members\": [{\"id\": \"" + incomingUserDataEntity.UserId + "\"}]}"; requestMessage.Content = new StringContent(payloadString, Encoding.UTF8, "application/json"); using (var sendResponse = await CompanyCommunicatorSendFunction.httpClient.SendAsync(requestMessage)) @@ -222,7 +223,6 @@ public async Task Run( else { await this.SaveSentNotificationData( - configuration, messageContent.NotificationId, incomingUserDataEntity.AadId, totalNumberOfThrottles, @@ -237,7 +237,7 @@ await this.SaveSentNotificationData( if (isCreateConversationThrottled) { - await this.SetDelayTimeAndSendDelayedRetry(configuration, messageContent); + await this.SetDelayTimeAndSendDelayedRetry(CompanyCommunicatorSendFunction.configuration, messageContent); return; } @@ -265,7 +265,6 @@ await this.SaveSentNotificationData( log.LogInformation("MESSAGE SENT SUCCESSFULLY"); saveSentNotificationDataTask = this.SaveSentNotificationData( - configuration, messageContent.NotificationId, incomingUserDataEntity.AadId, totalNumberOfThrottles, @@ -296,7 +295,6 @@ await this.SaveSentNotificationData( log.LogError($"MESSAGE FAILED: {sendResponse.StatusCode}"); saveSentNotificationDataTask = this.SaveSentNotificationData( - configuration, messageContent.NotificationId, incomingUserDataEntity.AadId, totalNumberOfThrottles, @@ -314,7 +312,7 @@ await this.SaveSentNotificationData( if (isSendMessageThrottled) { setDelayTimeAndSendDelayedRetryTask = - this.SetDelayTimeAndSendDelayedRetry(configuration, messageContent); + this.SetDelayTimeAndSendDelayedRetry(CompanyCommunicatorSendFunction.configuration, messageContent); } await Task.WhenAll( @@ -333,7 +331,6 @@ await Task.WhenAll( } await this.SaveSentNotificationData( - configuration, messageContent.NotificationId, messageContent.UserDataEntity.AadId, totalNumberOfThrottles, @@ -345,7 +342,6 @@ await this.SaveSentNotificationData( } private async Task SaveSentNotificationData( - IConfiguration configuration, string notificationId, string aadId, int totalNumberOfThrottles,