/// <summary>
        /// Checks to see if messaging providers are configured.
        /// </summary>
        /// <returns></returns>
        private async Task <bool> MessagingProvidersAreConfiguredAsync()
        {
            try
            {
                if (MessagingConnections != null && MessagingConnections.Count > 0)
                {
                    // if we have already setup the connections, then we are configured.
                    return(true);
                }
                else
                {
                    // otherwise check the database to see if we have any providers.

                    var messagingProviders = await Database.GetProvidersAsync(ProviderTypes.Messaging).ConfigureAwait(false);

                    if (messagingProviders.Count > 0)
                    {
                        // attemp to configure the providers.
                        var connections = new ProviderConnections(Database);
                        var messageProviderConnections = await connections.ConfigureMessagingProviderConnectionsAsync(Logger).ConfigureAwait(false);

                        if (messageProviderConnections != null)
                        {
                            MessagingConnections = messageProviderConnections;
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        // no providers setup yet.
                        Logger.WriteTraceMessage("No messaging providers have been configured yet. The status engine won't work until these have been configured.");
                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteTraceError("Failed to lookup or configure messaging providers.", ex, Logger.GenerateFullContextStackTrace());
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// Configures the messaging provider connections.
        /// </summary>
        /// <returns>True if successful, otherwise false.</returns>
        public async Task <MessagingProviderConnectionsCollection> ConfigureMessagingProviderConnectionsAsync(ILogger Log)
        {
            Log.WriteSystemEvent("Configuring messaging provider connections.", EventLogEntryType.Information, Constants.EventIDs.ConfiguringMessagingProviderConnections, true);

            var messagingConnections = new MessagingProviderConnectionsCollection();

            try
            {
                // establish the database and protected store.
                var settingName = Constants.RuntimeSettingNames.ProtectionIV;
                var protectionIvEncodedString = await Database.GetApplicationOptionAsync(settingName).ConfigureAwait(false);

                var ivBytes = Convert.FromBase64String(protectionIvEncodedString);

                ProtectedDataStore protectedStore = new ProtectedDataStore(Database, DataProtectionScope.LocalMachine, ivBytes);

                // configure the provider implementation instances.
                // add each to the collection of providers.

                var providersList = await Database.GetProvidersAsync(ProviderTypes.Messaging).ConfigureAwait(false);

                foreach (var provider in providersList)
                {
                    Log.WriteTraceMessage(string.Format("A messaging provider was found in the configuration database: Name: {0}", provider.Name));

                    switch (provider.Name)
                    {
                    case nameof(MessagingProviderTypes.Twilio):
                    {
                        Log.WriteTraceMessage("Checking for Twilio messaging provider connection settings.");
                        string accountID = await protectedStore.GetApplicationSecretAsync(Constants.RuntimeSettingNames.TwilioAccountID).ConfigureAwait(false);

                        string authToken = await protectedStore.GetApplicationSecretAsync(Constants.RuntimeSettingNames.TwilioAuthToken).ConfigureAwait(false);

                        string sourcePhone = await protectedStore.GetApplicationSecretAsync(Constants.RuntimeSettingNames.TwilioSourcePhone).ConfigureAwait(false);

                        string destPhones = await protectedStore.GetApplicationSecretAsync(Constants.RuntimeSettingNames.TwilioDestinationPhones).ConfigureAwait(false);

                        Log.WriteTraceMessage("Initializing Twilio messaging provider.");
                        var twilioConnection = new TwilioMessagingProviderOperations(Log, accountID, authToken, sourcePhone, destPhones);
                        messagingConnections.Add(MessagingProviderTypes.Twilio, twilioConnection);
                        Log.WriteTraceMessage("Successfully initialized the messaging provider.");

                        break;
                    }

                    default:
                    {
                        throw new NotImplementedException("Unexpected provider type specified: " + provider.Type.ToString());
                    }
                    }
                }

                if (messagingConnections.Count == 0)
                {
                    Log.WriteSystemEvent("No messaging providers are configured. This isn't a problem, but they are nice to have.", EventLogEntryType.Information, Constants.EventIDs.NoMessagingProviderConnections, true);
                    return(null);
                }
                else
                {
                    Log.WriteSystemEvent("Successfully configured messaging provider connections.", EventLogEntryType.Information, Constants.EventIDs.ConfiguredMessagingProviderConnections, true);
                    return(messagingConnections);
                }
            }
            catch (ApplicationCoreSettingMissingException ex)
            {
                Log.WriteSystemEvent("A core application setting has not been configured yet: " + ex.Message,
                                     EventLogEntryType.Error, Constants.EventIDs.CoreSettingMissing, true);

                return(null);
            }
            catch (ApplicationCoreSettingInvalidValueException ex)
            {
                Log.WriteSystemEvent("A core application setting has an invalid value specified: " + ex.Message,
                                     EventLogEntryType.Error, Constants.EventIDs.CoreSettingInvalid, true);

                return(null);
            }
            catch (ApplicationSecretMissingException)
            {
                Log.WriteSystemEvent("Failed to configure messaging provider connections: A messaging provider is missing required connection settings.",
                                     EventLogEntryType.Error, Constants.EventIDs.FailedToConfigureProvidersMissingSettings, true);

                return(null);
            }
            catch (Exception ex)
            {
                var message = "Failed to configure messaging provider connections.";
                var context = Log.GenerateFullContextStackTrace();
                Log.WriteSystemEvent(message, ex, context, Constants.EventIDs.FailedToConfigureMessagingProviderConnections, true);
                return(null);
            }
        }