1
- using Microsoft . AspNetCore . Builder ;
1
+ using System . Net . Mime ;
2
+ using Microsoft . AspNetCore . Builder ;
3
+ using Microsoft . AspNetCore . Diagnostics . HealthChecks ;
4
+ using Microsoft . AspNetCore . Http ;
5
+ using Microsoft . AspNetCore . Routing ;
6
+ using Microsoft . EntityFrameworkCore ;
7
+ using Microsoft . Extensions . Diagnostics . HealthChecks ;
2
8
using Microsoft . OpenApi . Any ;
3
9
using Microsoft . OpenApi . Models ;
10
+ using MySqlConnector ;
4
11
using NET6CustomLibrary . DateTime . Converters ;
5
12
using NET6CustomLibrary . Serilog . Services ;
6
13
using Serilog ;
@@ -109,4 +116,75 @@ public static IMvcBuilder AddSimpleJsonOptions(this IMvcBuilder builder)
109
116
return builder ;
110
117
}
111
118
#endregion
119
+
120
+ #region "EFCORE DBContext"
121
+ public static IServiceCollection AddDbContextUseMySql < TDbContext > ( this IServiceCollection services , string connectionString , int retryOnFailure ) where TDbContext : DbContext
122
+ {
123
+ services . AddDbContextPool < TDbContext > ( optionBuilder =>
124
+ {
125
+ if ( retryOnFailure > 0 )
126
+ {
127
+ optionBuilder . UseMySql ( connectionString , ServerVersion . AutoDetect ( connectionString ) , options =>
128
+ {
129
+ // Abilito il connection resiliency (Provider di Postgres è soggetto a errori transienti)
130
+ // Info su: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
131
+ options . EnableRetryOnFailure ( retryOnFailure ) ;
132
+ } ) ;
133
+ }
134
+ else
135
+ {
136
+ optionBuilder . UseMySql ( connectionString , ServerVersion . AutoDetect ( connectionString ) ) ;
137
+ }
138
+ } ) ;
139
+ return services ;
140
+ }
141
+ #endregion
142
+
143
+ #region "DATABASE HEALTH CHECKS"
144
+ public static IServiceCollection AddMySqlHealthChecks ( this IServiceCollection services , string connectionString )
145
+ {
146
+ services . AddHealthChecks ( )
147
+ . AddAsyncCheck ( "MySQL" , async ( ) =>
148
+ {
149
+ try
150
+ {
151
+ using var connection = new MySqlConnection ( connectionString ) ;
152
+ await connection . OpenAsync ( ) ;
153
+ }
154
+ catch ( Exception ex )
155
+ {
156
+ return HealthCheckResult . Unhealthy ( ex . Message , ex ) ;
157
+ }
158
+
159
+ return HealthCheckResult . Healthy ( ) ;
160
+ } ) ;
161
+
162
+ return services ;
163
+ }
164
+
165
+ public static IEndpointRouteBuilder AddDatabaseHealthChecks ( this IEndpointRouteBuilder builder )
166
+ {
167
+ builder . MapHealthChecks ( "/health" , new HealthCheckOptions
168
+ {
169
+ ResponseWriter = async ( context , report ) =>
170
+ {
171
+ var result = JsonSerializer . Serialize ( new
172
+ {
173
+ status = report . Status . ToString ( ) ,
174
+ details = report . Entries . Select ( e => new
175
+ {
176
+ service = e . Key ,
177
+ status = Enum . GetName ( typeof ( HealthStatus ) , e . Value . Status ) ,
178
+ description = e . Value . Description
179
+ } )
180
+ } ) ;
181
+
182
+ context . Response . ContentType = MediaTypeNames . Application . Json ;
183
+ await context . Response . WriteAsync ( result ) ;
184
+ }
185
+ } ) ;
186
+
187
+ return builder ;
188
+ }
189
+ #endregion
112
190
}
0 commit comments