-
Notifications
You must be signed in to change notification settings - Fork 25.3k
/
DbContextHealthStartup.cs
97 lines (83 loc) · 4.17 KB
/
DbContextHealthStartup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SampleApp.Data;
namespace SampleApp
{
// Use the `--scenario dbcontext` switch to run this version of the sample.
//
// Register Health Check Middleware at the URL: /health
//
// By default, health checks return a 200-Ok with 'Healthy' when the database is responsive.
// - A SqlConnectionHealthCheck is used in the example for a SQL database.
// - The default response writer writes the HealthStatus as text/plain content.
//
// AddDbContextCheck<TContext> registers a health check for the TContext type. By default, the name of the health check is the name of the TContext type. There are other options available through AddDbContextCheck to configure failure status, tags, and a custom test query.
public class DbContextHealthStartup
{
public DbContextHealthStartup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// <snippet_ConfigureServices>
services.AddHealthChecks()
.AddDbContextCheck<AppDbContext>();
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(
Configuration["ConnectionStrings:DefaultConnection"]);
});
// </snippet_ConfigureServices>
}
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health");
endpoints.MapGet("/createdatabase", async context =>
{
await context.Response.WriteAsync("Creating the database...");
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.Body.FlushAsync();
var appDbContext =
context.RequestServices.GetRequiredService<AppDbContext>();
await appDbContext.Database.EnsureCreatedAsync();
await context.Response.WriteAsync("Done!");
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync(
"Navigate to /health to see the health status.");
await context.Response.WriteAsync(Environment.NewLine);
});
endpoints.MapGet("deletedatabase", async context =>
{
await context.Response.WriteAsync("Deleting the database...");
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.Body.FlushAsync();
var appDbContext =
context.RequestServices.GetRequiredService<AppDbContext>();
await appDbContext.Database.EnsureDeletedAsync();
await context.Response.WriteAsync("Done!");
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync("Navigate to /health to see the health status.");
await context.Response.WriteAsync(Environment.NewLine);
});
endpoints.MapGet("/{**path}", async context =>
{
await context.Response.WriteAsync("Navigate to /health to see the health status.");
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync("Navigate to /createdatabase to create the database.");
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync("Navigate to /deletedatabase to delete the database.");
await context.Response.WriteAsync(Environment.NewLine);
});
});
}
}
}