Skip to content

Commit

Permalink
Add DbContextChecks to Health Checks (#648)
Browse files Browse the repository at this point in the history
This will ensure that the /healthcheck endpoint correctly reports the status of the service if a database connection faults
  • Loading branch information
DrizzlyOwl authored Dec 9, 2024
1 parent 70ee728 commit e89211e
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.1.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public static IServiceCollection AddInfrastructureDependencyGroup(
services.AddDbContext<EdperfContext>(options =>
options.UseSqlServer(connectionString));

AddInfrastructureHealthChecks(services);

return services;
}

Expand Down Expand Up @@ -58,10 +60,17 @@ public static IServiceCollection AddPersonsApiInfrastructureDependencyGroup(
services.AddDbContext<MopContext>(options =>
options.UseSqlServer(connectionString));

AddInfrastructureHealthChecks(services);

// Authentication
services.AddCustomAuthorization(config);

return services;
}

public static void AddInfrastructureHealthChecks(this IServiceCollection services) {
services.AddHealthChecks()
.AddDbContextCheck<MstrContext>("Academies Database");
}
}
}
}
2 changes: 1 addition & 1 deletion PersonsApi/PersonsApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@
<Message Text="$(NSwagOutput)" Condition="'$(NSwagExitCode)' == '0'" Importance="low" />
<Error Text="$(NSwagOutput)" Condition="'$(NSwagExitCode)' != '0'" />
</Target>

</Project>
2 changes: 2 additions & 0 deletions PersonsApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@

logger.LogInformation("Logger is working...");

app.MapHealthChecks("/HealthCheck").AllowAnonymous();

app.Run();
5 changes: 2 additions & 3 deletions PersonsApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public void ConfigureServices(IServiceCollection services)
{
configure.Title = "PersonsApi";
});

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down Expand Up @@ -175,10 +174,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVers
app.UseHttpsRedirection();
app.UseRouting();

app.UseAuthentication();
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
}
}
62 changes: 0 additions & 62 deletions TramsDataApi/Controllers/HealthCheckController.cs

This file was deleted.

2 changes: 2 additions & 0 deletions TramsDataApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@

logger.LogInformation("Logger is working...");

app.MapHealthChecks("/HealthCheck").AllowAnonymous();

app.Run();

0 comments on commit e89211e

Please sign in to comment.