public async Task Subscribe(string endpointName, Action <IRoutableMessage> MessageHandler, Func <Exception, Task> ErrorHandler) { // Get the subscription information from the endpointName setting in the configuration file IApplicationSecretsConnectionStrings subscriber = _applicationSecrets.Secret(endpointName); string connectionString = subscriber.Value; string topicName = subscriber[Topic]; string subscriptionName = subscriber[Subscription]; string path = EntityNameHelper.FormatSubscriptionPath(topicName, subscriptionName); var receiver = new MessageReceiver(connectionString, path); receiver.RegisterMessageHandler( async(message, cancellationToken) => { await receiver.CompleteAsync(message.SystemProperties.LockToken); string body = Encoding.UTF8.GetString(message.Body); IRoutableMessage msg = body.MessageFromBus(); MessageHandler(msg); }, new MessageHandlerOptions(e => ErrorHandler(e.Exception)) { AutoComplete = false, MaxConcurrentCalls = 2 }); }
public static bool RefreshConfigurationFromCache(this IRedisCache cache, IApplicationSecrets secrets, IConfigurationRoot configuration) { if (!Initialized) { // Get the information about what cache values we need refreshed into configuration IApplicationSecretsConnectionStrings TimedCacheRefresh = secrets.Secret("TimedCacheRefresh"); if (TimedCacheRefresh != null) { // Use the "TimedCacheRefresh" secret to get the list of cache keys that need to be auto-refreshed // and placed into the contiguration. This allows the application to read cache values just like // regular configuration settings in "appsettings.json". The "Value" for this secret contains // an array of cache keys that must be refreshed periodically. string[] keys = TimedCacheRefresh.Value.Split(','); // The MetadataProperties "RefreshPeriodMinutes" contains the refresh period for the cache keys string RefreshPeriodMinutes = TimedCacheRefresh["RefreshPeriodMinutes"]; if (!string.IsNullOrWhiteSpace(RefreshPeriodMinutes)) { int minutes = int.Parse(RefreshPeriodMinutes); // Start the thread that will read the cache every N minutes Task task = new Task(() => LaunchTimedRefresh(cache, keys, minutes, configuration)); task.Start(); // Wait for the thread to read all cache keys for the first time before continuing waitForCompletion.WaitOne(); Initialized = true; } } } return(Initialized); }
protected BlobContainerClient GetContainerClient() { // Uses the IApplicationSecrets interface to retrieve all the data related to the secret "BlobStorage" IApplicationSecretsConnectionStrings secret = _applicationSecrets.Secret("BlobStorage"); string blobConnectionString = secret.Value; string containerName = secret[ContainerName]; // Create a BlobServiceClient object which will be used to get a container BlobServiceClient blobServiceClient = new BlobServiceClient(blobConnectionString); // Get the container containing the blob BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); return (containerClient); }
/// <summary> /// This is the application entry point. /// </summary> /// <returns></returns> internal async Task Run() { $"Application Started at {DateTime.Now.ToLongTimeString()}".TraceInformation(); _InitialConfiguration.TraceInformation("Dumping InitialConfiguration"); _ApplicationSecrets.TraceInformation("Dumping ApplicationSecrets"); // Demonstrate how to get at any connection string string FileLoggerConnectionString = _ApplicationSecrets.ConnectionString("FileLogger"); if (!string.IsNullOrEmpty(FileLoggerConnectionString)) { FileLoggerConnectionString.TraceInformation("FileLogger connection string value"); // Demonstrate how to get ENTIRE secret, including description and metadata IApplicationSecretsConnectionStrings FileLoggerSecret = _ApplicationSecrets.Secret("FileLogger"); if (FileLoggerSecret != null) { FileLoggerSecret.TraceInformation("FileLogger ENTIRE secret"); if (FileLoggerSecret.MetaDataProperties != null) { foreach (SecretMetaData metaData in FileLoggerSecret.MetaDataProperties) { metaData.TraceInformation("MetaData"); } } } } // Display the JWT token that was read from Redis Cache string JWT = _Configuration["ONIT_JWT"]; if (!string.IsNullOrEmpty(JWT)) { JWT.TraceInformation("JWT token from cache"); } else { "No JWT token was found".TraceInformation(); } $"Application Ended at {DateTime.Now.ToLongTimeString()}".TraceInformation(); Console.WriteLine("PRESS <ENTER> TO EXIT"); Console.ReadKey(); }
/// <summary> /// The appsettings section "ApplicationSecrets" contains all connection string and sensitive information. /// To hide this information from source control and to allow individual developers to have their own settings /// we copy the section "ApplicationSecrets" into the secrets.json file for local development. /// In production this value will come from KeyVault. This method reads the appropriate values /// can generates the final IApplicationSecrets that will be used at runtime. /// </summary> /// <param name="configuration">IConfigurationRoot</param> /// <param name="applicationSetupConfiguration">IApplicationSetupConfiguration</param> /// <returns>IApplicationSecrets containing contents of the "ApplicationSecrets" section of configuration</returns> public static IApplicationSecrets InitializeApplicationSecrets(IConfigurationRoot configuration, IApplicationSetupConfiguration applicationSetupConfiguration) { ApplicationSecrets retVal = null; try { if (!string.IsNullOrEmpty(applicationSetupConfiguration.KeyVaultKey)) { string mySecret = configuration[applicationSetupConfiguration.KeyVaultKey]; string decoded = Base64Decode(mySecret); JObject jo = JObject.Parse(decoded); string val = jo.Properties().First(x => x.Name == ApplicationSecretsSectionName).Value.ToString(); retVal = JsonConvert.DeserializeObject <ApplicationSecrets>(val); } } catch (Exception Err) { } // Bind the local configuration properties to the properties in the ApplicationSecrets object IConfigurationSection myConfiguration = configuration.GetSection(ApplicationSecretsSectionName); ApplicationSecrets localSecrets = new ApplicationSecrets(); myConfiguration.Bind(localSecrets); // If the local configuration contains secrets that were not present in KeyVault, then include them if (retVal != null) { foreach (ApplicationSecretsConnectionStrings nextSecret in localSecrets.ConnectionStrings) { // Try to find the local secret name in the KeyVault version. If not found in KeyVault, then insert it // into final merge. IApplicationSecretsConnectionStrings found = retVal.Secret(nextSecret.Name); if (found == null) { retVal.ConnectionStrings.Add(nextSecret); } } } else { retVal = localSecrets; } return(retVal); }
public async Task Publish(IRoutableMessage message) { try { // Get the connection string from the destination address in the message IApplicationSecretsConnectionStrings publisher = _applicationSecrets.Secret(message.Recipient); // Create a sender for the topic string topicName = publisher[Topic]; string connectionString = publisher.Value; Message serviceBusMessage = new Message(Encoding.UTF8.GetBytes(message.ToString())); ISenderClient topicClient = new TopicClient(connectionString, topicName); await topicClient.SendAsync(serviceBusMessage); } catch (Exception e) { Console.WriteLine(e); throw; } }