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

Commit 00cdf1d

Browse files
committed
Aggiunto SQL Server Health Check
1 parent 9e92fae commit 00cdf1d

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/NET6CustomLibrary/Extensions/DependencyInjection.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ public static IServiceCollection AddPostgresHealthChecks(this IServiceCollection
176176
return services;
177177
}
178178

179+
public static IServiceCollection AddSqlServerHealthChecks(this IServiceCollection services, string connectionString, string nameAsyncCheck)
180+
{
181+
services.AddHealthChecks()
182+
.AddAsyncCheck(nameAsyncCheck, async () =>
183+
{
184+
try
185+
{
186+
using var connection = new SqlConnection(connectionString);
187+
await connection.OpenAsync();
188+
}
189+
catch (Exception ex)
190+
{
191+
return HealthCheckResult.Unhealthy(ex.Message, ex);
192+
}
193+
194+
return HealthCheckResult.Healthy();
195+
});
196+
197+
return services;
198+
}
199+
179200
public static IEndpointRouteBuilder AddDatabaseHealthChecks(this IEndpointRouteBuilder builder, string pattern)
180201
{
181202
builder.MapHealthChecks(pattern, new HealthCheckOptions

src/NET6CustomLibrary/GlobalUsings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
global using Microsoft.AspNetCore.Http;
1717
global using Microsoft.AspNetCore.Identity.UI.Services;
1818
global using Microsoft.AspNetCore.Routing;
19+
global using Microsoft.Data.SqlClient;
1920
global using Microsoft.EntityFrameworkCore;
2021
global using Microsoft.Extensions.Configuration;
2122
global using Microsoft.Extensions.DependencyInjection;

src/NET6CustomLibrary/NET6CustomLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<PackageReference Include="MailKit" Version="3.6.0" />
2020
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.15" />
2121
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
22+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
2223
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
2324
<PrivateAssets>all</PrivateAssets>
2425
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)