Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 44e5484

Browse files
committed
Aggiunto DbContextPool e MySql HealthChecks
1 parent 4422aaa commit 44e5484

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/NET6CustomLibrary/Extensions/DependencyInjection.cs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
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;
28
using Microsoft.OpenApi.Any;
39
using Microsoft.OpenApi.Models;
10+
using MySqlConnector;
411
using NET6CustomLibrary.DateTime.Converters;
512
using NET6CustomLibrary.Serilog.Services;
613
using Serilog;
@@ -109,4 +116,75 @@ public static IMvcBuilder AddSimpleJsonOptions(this IMvcBuilder builder)
109116
return builder;
110117
}
111118
#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
112190
}

src/NET6CustomLibrary/NET6CustomLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<PrivateAssets>all</PrivateAssets>
3030
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3131
</PackageReference>
32+
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
3233
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
3334
</ItemGroup>
3435

0 commit comments

Comments
 (0)