Skip to content

Commit 3b0875f

Browse files
add health check and health check ui to project.
1 parent 6f4f5be commit 3b0875f

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/Web/Api/CleanTemplate.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15+
1516
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
1617
</ItemGroup>
1718

src/Web/Api/DependencyInjection.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
using Domain.IRepositories;
1212
using FluentValidation;
1313
using FluentValidation.AspNetCore;
14+
using HealthChecks.UI.Client;
1415
using MediatR;
1516
using Microsoft.AspNetCore.Authentication.JwtBearer;
1617
using Microsoft.AspNetCore.Builder;
18+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
1719
using Microsoft.AspNetCore.Identity;
1820
using Microsoft.AspNetCore.Mvc;
1921
using Microsoft.Extensions.Configuration;
@@ -37,6 +39,7 @@ public static class DependencyInjection
3739
public static IServiceCollection AddWebApi(this IServiceCollection services, IConfiguration configuration, SiteSettings siteSettings)
3840
{
3941
services.Configure<SiteSettings>(configuration.GetSection(nameof(SiteSettings)));
42+
var appOptions = configuration.GetSection(nameof(AppOptions)).Get<AppOptions>();
4043

4144
services.AddApiVersioning(o =>
4245
{
@@ -54,6 +57,10 @@ public static IServiceCollection AddWebApi(this IServiceCollection services, ICo
5457
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
5558
services.AddPolyCache(configuration);
5659

60+
services.AddHealthChecks();
61+
services.AddHealthChecksUI()
62+
.AddInMemoryStorage();
63+
5764
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(PerformanceBehaviour<,>));
5865
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
5966
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>));
@@ -81,6 +88,11 @@ public static IApplicationBuilder UseWebApi(this IApplicationBuilder app, IConfi
8188
app.UseEndpoints(endpoints =>
8289
{
8390
endpoints.MapControllers();
91+
endpoints.MapHealthChecksUI();
92+
endpoints.MapHealthChecks("/health", new HealthCheckOptions()
93+
{
94+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
95+
});
8496
});
8597

8698
return app;

src/Web/Api/appsettings.Development.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,14 @@
6363
}
6464
}
6565
]
66+
},
67+
"HealthChecksUI": {
68+
"HealthChecks": [
69+
{
70+
"Name": "Clean Template Project Service",
71+
"Uri": "https://localhost:44398/health"
72+
}
73+
],
74+
"EvaluationTimeInSeconds": 5
6675
}
6776
}

src/Web/ApiFramework/CleanTemplate.ApiFramework.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="6.0.2" />
9+
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="6.0.4" />
10+
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="6.0.4" />
11+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.4" />
812
<PackageReference Include="Autofac" Version="6.3.0" />
913
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
14+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
1015
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0" />
1116
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
1217
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />

0 commit comments

Comments
 (0)