Example #1
0
        public static IMetricsClient GenerateClient()
        {
            Mock <ILoggerFactory> factory = new Mock <ILoggerFactory>();
            var metricsClientFactory      = new MetricsClientFactory(factory.Object,
                                                                     TimeSpan.FromSeconds(15));

            return(metricsClientFactory.CreateMetricsClient <LoggerMetricsClient>());
        }
Example #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            var azureStorageSettings    = GetSettings <AzureStorageSettings>(Configuration);
            var azureServiceBusSettings = GetSettings <AzureServiceBusSettings>(Configuration);
            var resiliencyParameters    = GetSettings <ResiliencyParameters>(Configuration);

            var profileCloudTable           = new AzureCloudTable(azureStorageSettings.ConnectionString, azureStorageSettings.ProfilesTableName);
            var profileStore                = new AzureTableProfileStore(profileCloudTable);
            var messagesCloudTable          = new AzureCloudTable(azureStorageSettings.ConnectionString, azureStorageSettings.MessagesTable);
            var userConversationsCloudTable = new AzureCloudTable(azureStorageSettings.ConnectionString, azureStorageSettings.UserConversationsTable);
            var conversationStore           = new AzureConversationStore(messagesCloudTable, userConversationsCloudTable);

            services.AddSingleton <IQueueClient>(new QueueClient(azureServiceBusSettings.ConnectionString,
                                                                 azureServiceBusSettings.QueueName));



            services.AddSingleton <INotificationServiceClient>(context =>
                                                               new NotificationClientResiliencyDecorator(
                                                                   new NotificationServiceBusClient(context.GetRequiredService <IQueueClient>()),
                                                                   new PollyResiliencyPolicy <Exception>(resiliencyParameters)));

            services.AddSingleton <IMetricsClient>(context =>
            {
                var metricsClientFactory = new MetricsClientFactory(context.GetRequiredService <ILoggerFactory>(),
                                                                    TimeSpan.FromSeconds(15));
                return(metricsClientFactory.CreateMetricsClient <LoggerMetricsClient>());
            });

            services.AddSingleton <IProfileStore>(context =>
                                                  new ProfileStoreResiliencyDecorator(
                                                      new ProfileStoreMetricDecorator(profileStore, context.GetRequiredService <IMetricsClient>()),
                                                      new PollyResiliencyPolicy <StorageErrorException>(resiliencyParameters)));

            services.AddSingleton <IConversationStore>(context =>
                                                       new ConversationStoreResiliencyDecorator(
                                                           new ConversationStoreMetricDecorator(conversationStore, context.GetRequiredService <IMetricsClient>()),
                                                           new PollyResiliencyPolicy <StorageUnavailableException>(resiliencyParameters)));

            services.AddLogging();
            services.AddMvc();
        }
Example #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.AddApiVersioning(
                o =>
            {
                o.AssumeDefaultVersionWhenUnspecified = true;
                o.DefaultApiVersion = new ApiVersion(1, 0);
            });

            var azureStorageSettings        = GetSettings <AzureStorageSettings>();
            var notificationServiceSettings = GetSettings <NotificationServiceSettings>();
            var faultToleranceSettings      = GetSettings <FaultToleranceSettings>();

            services.AddSingleton <IMetricsClient>(context =>
            {
                var metricsClientFactory = new MetricsClientFactory(context.GetRequiredService <ILoggerFactory>(),
                                                                    TimeSpan.FromSeconds(15));
                return(metricsClientFactory.CreateMetricsClient <LoggerMetricsClient>());
            });

            TimeoutPolicy        timeoutPolicy        = Policy.Timeout(faultToleranceSettings.TimeoutLength, TimeoutStrategy.Pessimistic);
            CircuitBreakerPolicy circuitBreakerPolicy = Policy
                                                        .Handle <Exception>()
                                                        .CircuitBreaker(
                exceptionsAllowedBeforeBreaking: faultToleranceSettings.ExceptionsAllowedBeforeBreaking,
                durationOfBreak: TimeSpan.FromMinutes(faultToleranceSettings.DurationOfBreakInMinutes)
                );
            PolicyWrap policyWrap = Policy.Wrap(circuitBreakerPolicy, timeoutPolicy);

            services.AddSingleton <ISyncPolicy>(policyWrap);

            QueueClient queueClient = new QueueClient(notificationServiceSettings.ServiceBusConnectionString,
                                                      notificationServiceSettings.QueueName);
            ServiceBusNotificationServiceClient notificationService = new ServiceBusNotificationServiceClient(queueClient);

            services.AddSingleton <INotificationService>(context =>
                                                         new NotificationServiceMetricsDecorator(
                                                             new NotificationServiceFaultToleranceDecorator(
                                                                 notificationService,
                                                                 context.GetRequiredService <ISyncPolicy>()),
                                                             context.GetRequiredService <IMetricsClient>()));

            AzureCloudTable        profileCloudTable = new AzureCloudTable(azureStorageSettings.ConnectionString, azureStorageSettings.ProfilesTableName);
            AzureTableProfileStore profileStore      = new AzureTableProfileStore(profileCloudTable);

            services.AddSingleton <IProfileStore>(context =>
                                                  new ProfileStoreMetricsDecorator(
                                                      new ProfileStoreFaultToleranceDecorator(
                                                          profileStore,
                                                          context.GetRequiredService <ISyncPolicy>()),
                                                      context.GetRequiredService <IMetricsClient>()));

            AzureCloudTable         messagesCloudTable = new AzureCloudTable(azureStorageSettings.ConnectionString, azureStorageSettings.MessagesTableName);
            AzureTableMessagesStore messagesStore      = new AzureTableMessagesStore(messagesCloudTable);

            services.AddSingleton <IMessagesStore>(context =>
                                                   new MessagesStoreMetricsDecorator(
                                                       new MessagesStoreFaultToleranceDecorator(
                                                           messagesStore,
                                                           context.GetRequiredService <ISyncPolicy>()),
                                                       context.GetRequiredService <IMetricsClient>()));

            AzureCloudTable conversationsCloudTable         = new AzureCloudTable(azureStorageSettings.ConnectionString, azureStorageSettings.UsersTableName);
            AzureTableConversationsStore conversationsStore = new AzureTableConversationsStore(conversationsCloudTable, messagesStore);

            services.AddSingleton <IConversationsStore>(context =>
                                                        new ConversationStoreMetricsDecorator(
                                                            new ConversationsStoreFaultToleranceDecorator(
                                                                conversationsStore,
                                                                context.GetRequiredService <ISyncPolicy>()),
                                                            context.GetRequiredService <IMetricsClient>()));

            services.AddSingleton <IConversationService>(context =>
                                                         new ConversationServiceMetricsDecorator(
                                                             new ConversationService(
                                                                 context.GetRequiredService <IConversationsStore>(),
                                                                 context.GetRequiredService <ILogger <ConversationService> >(),
                                                                 context.GetRequiredService <INotificationService>()),
                                                             context.GetRequiredService <IMetricsClient>()));

            services.AddLogging();
            services.AddMvc();
        }