1- using System . Linq ;
1+ using Basket . API . Infrastructure . Filters ;
2+ using Basket . API . IntegrationEvents . EventHandling ;
3+ using Basket . API . IntegrationEvents . Events ;
24using Microsoft . AspNetCore . Builder ;
35using Microsoft . AspNetCore . Hosting ;
6+ using Microsoft . eShopOnContainers . BuildingBlocks . EventBus . Abstractions ;
7+ using Microsoft . eShopOnContainers . BuildingBlocks . EventBusRabbitMQ ;
8+ using Microsoft . eShopOnContainers . Services . Basket . API . Auth . Server ;
9+ using Microsoft . eShopOnContainers . Services . Basket . API . IntegrationEvents . EventHandling ;
10+ using Microsoft . eShopOnContainers . Services . Basket . API . IntegrationEvents . Events ;
11+ using Microsoft . eShopOnContainers . Services . Basket . API . Model ;
412using Microsoft . Extensions . Configuration ;
513using Microsoft . Extensions . DependencyInjection ;
14+ using Microsoft . Extensions . HealthChecks ;
615using Microsoft . Extensions . Logging ;
7- using Microsoft . eShopOnContainers . Services . Basket . API . Model ;
8- using StackExchange . Redis ;
916using Microsoft . Extensions . Options ;
17+ using StackExchange . Redis ;
18+ using System . Linq ;
1019using System . Net ;
11- using Microsoft . eShopOnContainers . Services . Basket . API . Auth . Server ;
12- using Microsoft . eShopOnContainers . BuildingBlocks . EventBus . Abstractions ;
13- using Microsoft . eShopOnContainers . Services . Basket . API . IntegrationEvents . Events ;
14- using Microsoft . eShopOnContainers . Services . Basket . API . IntegrationEvents . EventHandling ;
15- using Microsoft . eShopOnContainers . BuildingBlocks . EventBusRabbitMQ ;
16- using System ;
17- using Microsoft . Extensions . HealthChecks ;
1820using System . Threading . Tasks ;
19- using Basket . API . Infrastructure . Filters ;
20- using Basket . API . IntegrationEvents . Events ;
21- using Basket . API . IntegrationEvents . EventHandling ;
2221
2322namespace Microsoft . eShopOnContainers . Services . Basket . API
2423{
@@ -59,17 +58,24 @@ public void ConfigureServices(IServiceCollection services)
5958 //and then creating the connection it seems reasonable to move
6059 //that cost to startup instead of having the first request pay the
6160 //penalty.
62- services . AddSingleton < ConnectionMultiplexer > ( ( sp ) => {
63- var config = sp . GetRequiredService < IOptionsSnapshot < BasketSettings > > ( ) . Value ;
64- var ips = Dns . GetHostAddressesAsync ( config . ConnectionString ) . Result ;
61+ services . AddSingleton < ConnectionMultiplexer > ( sp => {
62+ var settings = sp . GetRequiredService < IOptions < BasketSettings > > ( ) . Value ;
63+ var ips = Dns . GetHostAddressesAsync ( settings . ConnectionString ) . Result ;
64+
6565 return ConnectionMultiplexer . Connect ( ips . First ( ) . ToString ( ) ) ;
6666 } ) ;
6767
68+ services . AddSingleton < IEventBus > ( sp =>
69+ {
70+ var settings = sp . GetRequiredService < IOptions < BasketSettings > > ( ) . Value ;
71+
72+ return new EventBusRabbitMQ ( settings . EventBusConnection ) ;
73+ } ) ;
74+
6875 services . AddSwaggerGen ( ) ;
69- //var sch = new IdentitySecurityScheme();
76+
7077 services . ConfigureSwaggerGen ( options =>
7178 {
72- //options.AddSecurityDefinition("IdentityServer", sch);
7379 options . OperationFilter < AuthorizationHeaderParameterOperationFilter > ( ) ;
7480 options . DescribeAllEnumsAsStrings ( ) ;
7581 options . SingleApiVersion ( new Swashbuckle . Swagger . Model . Info ( )
@@ -95,10 +101,7 @@ public void ConfigureServices(IServiceCollection services)
95101 services . AddTransient < IIntegrationEventHandler < ProductPriceChangedIntegrationEvent > , ProductPriceChangedIntegrationEventHandler > ( ) ;
96102 services . AddTransient < IIntegrationEventHandler < OrderStartedIntegrationEvent > , OrderStartedIntegrationEventHandler > ( ) ;
97103
98- var serviceProvider = services . BuildServiceProvider ( ) ;
99- var configuration = serviceProvider . GetRequiredService < IOptionsSnapshot < BasketSettings > > ( ) . Value ;
100- services . AddSingleton < IEventBus > ( provider => new EventBusRabbitMQ ( configuration . EventBusConnection ) ) ;
101-
104+
102105 }
103106
104107 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -119,11 +122,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
119122 app . UseSwagger ( )
120123 . UseSwaggerUi ( ) ;
121124
122- var catalogPriceHandler = app . ApplicationServices . GetService < IIntegrationEventHandler < ProductPriceChangedIntegrationEvent > > ( ) ;
123- var orderStartedHandler = app . ApplicationServices . GetService < IIntegrationEventHandler < OrderStartedIntegrationEvent > > ( ) ;
124- var eventBus = app . ApplicationServices . GetRequiredService < IEventBus > ( ) ;
125- eventBus . Subscribe < ProductPriceChangedIntegrationEvent > ( catalogPriceHandler ) ;
126- eventBus . Subscribe < OrderStartedIntegrationEvent > ( orderStartedHandler ) ;
125+ ConfigureEventBus ( app ) ;
127126
128127 }
129128
@@ -136,6 +135,21 @@ protected virtual void ConfigureAuth(IApplicationBuilder app)
136135 ScopeName = "basket" ,
137136 RequireHttpsMetadata = false
138137 } ) ;
139- }
138+ }
139+
140+ protected virtual void ConfigureEventBus ( IApplicationBuilder app )
141+ {
142+ var catalogPriceHandler = app . ApplicationServices
143+ . GetService < IIntegrationEventHandler < ProductPriceChangedIntegrationEvent > > ( ) ;
144+
145+ var orderStartedHandler = app . ApplicationServices
146+ . GetService < IIntegrationEventHandler < OrderStartedIntegrationEvent > > ( ) ;
147+
148+ var eventBus = app . ApplicationServices
149+ . GetRequiredService < IEventBus > ( ) ;
150+
151+ eventBus . Subscribe < ProductPriceChangedIntegrationEvent > ( catalogPriceHandler ) ;
152+ eventBus . Subscribe < OrderStartedIntegrationEvent > ( orderStartedHandler ) ;
153+ }
140154 }
141155}
0 commit comments