@@ -26,11 +26,14 @@ namespace TransactionProcessor
2626 using Common ;
2727 using EstateManagement . Client ;
2828 using EventStore . Client ;
29+ using HealthChecks . UI . Client ;
2930 using MediatR ;
3031 using MessagingService . BusinessLogic . EventHandling ;
3132 using Microsoft . AspNetCore . Authentication . JwtBearer ;
33+ using Microsoft . AspNetCore . Diagnostics . HealthChecks ;
3234 using Microsoft . AspNetCore . Mvc . ApiExplorer ;
3335 using Microsoft . AspNetCore . Mvc . Versioning ;
36+ using Microsoft . Extensions . Diagnostics . HealthChecks ;
3437 using Microsoft . Extensions . Options ;
3538 using Microsoft . IdentityModel . Logging ;
3639 using Models ;
@@ -69,6 +72,10 @@ public Startup(IWebHostEnvironment webHostEnvironment)
6972 // This method gets called by the runtime. Use this method to add services to the container.
7073 public void ConfigureServices ( IServiceCollection services )
7174 {
75+ ConfigurationReader . Initialise ( Startup . Configuration ) ;
76+
77+ Startup . ConfigureEventStoreSettings ( ) ;
78+
7279 this . ConfigureMiddlewareServices ( services ) ;
7380
7481 services . AddTransient < IMediator , Mediator > ( ) ;
@@ -107,51 +114,8 @@ public void ConfigureServices(IServiceCollection services)
107114 }
108115 else
109116 {
110- services . AddEventStoreClient ( ( settings ) =>
111- {
112- settings . CreateHttpMessageHandler = ( ) => new SocketsHttpHandler
113- {
114- SslOptions =
115- {
116- RemoteCertificateValidationCallback = ( sender ,
117- certificate ,
118- chain ,
119- errors ) => true ,
120- }
121- } ;
122- settings . ConnectionName = Startup . Configuration . GetValue < String > ( "EventStoreSettings:ConnectionName" ) ;
123- settings . ConnectivitySettings = new EventStoreClientConnectivitySettings
124- {
125- Address =
126- new Uri ( Startup . Configuration . GetValue < String > ( "EventStoreSettings:ConnectionString" ) ) ,
127- } ;
128- settings . DefaultCredentials = new UserCredentials ( Startup . Configuration . GetValue < String > ( "EventStoreSettings:UserName" ) ,
129- Startup . Configuration . GetValue < String > ( "EventStoreSettings:Password" ) ) ;
130- } ) ;
131-
132-
133-
134- services . AddEventStoreProjectionManagerClient ( ( settings ) =>
135- {
136- settings . CreateHttpMessageHandler = ( ) => new SocketsHttpHandler
137- {
138- SslOptions =
139- {
140- RemoteCertificateValidationCallback = ( sender ,
141- certificate ,
142- chain ,
143- errors ) => true ,
144- }
145- } ;
146- settings . ConnectionName = Startup . Configuration . GetValue < String > ( "EventStoreSettings:ConnectionName" ) ;
147- settings . ConnectivitySettings = new EventStoreClientConnectivitySettings
148- {
149- Address =
150- new Uri ( Startup . Configuration . GetValue < String > ( "EventStoreSettings:ConnectionString" ) )
151- } ;
152- settings . DefaultCredentials = new UserCredentials ( Startup . Configuration . GetValue < String > ( "EventStoreSettings:UserName" ) ,
153- Startup . Configuration . GetValue < String > ( "EventStoreSettings:Password" ) ) ;
154- } ) ;
117+ services . AddEventStoreClient ( Startup . ConfigureEventStoreSettings ) ;
118+ services . AddEventStoreProjectionManagerClient ( Startup . ConfigureEventStoreSettings ) ;
155119 }
156120
157121 services . AddTransient < IEventStoreContext , EventStoreContext > ( ) ;
@@ -206,10 +170,55 @@ public void ConfigureServices(IServiceCollection services)
206170 services . AddSingleton < IFeeCalculationManager , FeeCalculationManager > ( ) ;
207171 }
208172
209-
173+ private static EventStoreClientSettings EventStoreClientSettings ;
174+
175+ private static void ConfigureEventStoreSettings ( EventStoreClientSettings settings = null )
176+ {
177+ if ( settings == null )
178+ {
179+ settings = new EventStoreClientSettings ( ) ;
180+ }
181+
182+ settings . CreateHttpMessageHandler = ( ) => new SocketsHttpHandler
183+ {
184+ SslOptions =
185+ {
186+ RemoteCertificateValidationCallback = ( sender ,
187+ certificate ,
188+ chain ,
189+ errors ) => true ,
190+ }
191+ } ;
192+ settings . ConnectionName = Startup . Configuration . GetValue < String > ( "EventStoreSettings:ConnectionName" ) ;
193+ settings . ConnectivitySettings = new EventStoreClientConnectivitySettings
194+ {
195+ Address = new Uri ( Startup . Configuration . GetValue < String > ( "EventStoreSettings:ConnectionString" ) ) ,
196+ } ;
197+
198+ settings . DefaultCredentials = new UserCredentials ( Startup . Configuration . GetValue < String > ( "EventStoreSettings:UserName" ) ,
199+ Startup . Configuration . GetValue < String > ( "EventStoreSettings:Password" ) ) ;
200+ Startup . EventStoreClientSettings = settings ;
201+ }
210202
211203 private void ConfigureMiddlewareServices ( IServiceCollection services )
212204 {
205+ services . AddHealthChecks ( )
206+ . AddEventStore ( Startup . EventStoreClientSettings ,
207+ userCredentials : Startup . EventStoreClientSettings . DefaultCredentials ,
208+ name : "Eventstore" ,
209+ failureStatus : HealthStatus . Unhealthy ,
210+ tags : new string [ ] { "db" , "eventstore" } )
211+ . AddUrlGroup ( new Uri ( $ "{ ConfigurationReader . GetValue ( "SecurityConfiguration" , "Authority" ) } /.well-known/openid-configuration") ,
212+ name : "Security Service" ,
213+ httpMethod : HttpMethod . Get ,
214+ failureStatus : HealthStatus . Unhealthy ,
215+ tags : new string [ ] { "security" , "authorisation" } )
216+ . AddUrlGroup ( new Uri ( $ "{ ConfigurationReader . GetValue ( "AppSettings" , "EstateManagementApi" ) } /.well-known/openid-configuration") ,
217+ name : "Estate Management Service" ,
218+ httpMethod : HttpMethod . Get ,
219+ failureStatus : HealthStatus . Unhealthy ,
220+ tags : new string [ ] { "application" , "estatemanagement" } ) ;
221+
213222 services . AddApiVersioning (
214223 options =>
215224 {
@@ -299,8 +308,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
299308
300309 Logger . Initialise ( logger ) ;
301310
302- ConfigurationReader . Initialise ( Startup . Configuration ) ;
303-
304311 app . AddRequestLogging ( ) ;
305312 app . AddResponseLogging ( ) ;
306313 app . AddExceptionHandler ( ) ;
@@ -313,6 +320,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
313320 app . UseEndpoints ( endpoints =>
314321 {
315322 endpoints . MapControllers ( ) ;
323+ endpoints . MapHealthChecks ( "health" , new HealthCheckOptions ( )
324+ {
325+ Predicate = _ => true ,
326+ ResponseWriter = UIResponseWriter . WriteHealthCheckUIResponse
327+ } ) ;
316328 } ) ;
317329
318330 app . UseSwagger ( ) ;
@@ -326,17 +338,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
326338 options . SwaggerEndpoint ( $ "/swagger/{ description . GroupName } /swagger.json", description . GroupName . ToUpperInvariant ( ) ) ;
327339 }
328340 } ) ;
329-
330- //if (String.Compare(ConfigurationReader.GetValue("EventStoreSettings", "START_PROJECTIONS"),
331- // Boolean.TrueString,
332- // StringComparison.InvariantCultureIgnoreCase) == 0)
333- //{
334- // app.PreWarm(true).Wait();
335- //}
336- //else
337- //{
338- // app.PreWarm();
339- //}
340341 }
341342 }
342343}
0 commit comments