-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathProgram.cs
89 lines (69 loc) · 3.38 KB
/
Program.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
using Microsoft.EntityFrameworkCore.Infrastructure;
using Steeltoe.Configuration.CloudFoundry;
using Steeltoe.Connectors.EntityFrameworkCore.MySql;
using Steeltoe.Connectors.MySql;
using Steeltoe.Management.Endpoint.Actuators.All;
using Steeltoe.Samples.MySqlEFCore;
using Steeltoe.Samples.MySqlEFCore.Data;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
// Steeltoe: Add Cloud Foundry Configuration Provider for Actuator integration (not required for connectors).
builder.AddCloudFoundryConfiguration();
// Steeltoe: Add actuator endpoints.
builder.Services.AddAllActuators();
// Steeltoe: Setup MySQL options, connection factory and health checks.
builder.AddMySql();
// Steeltoe: Review appsettings.development.json to see how local connection strings are provided.
bool useMultipleDatabases = builder.Configuration.GetValue("useMultipleDatabases", false);
if (useMultipleDatabases)
{
// Steeltoe: When using multiple databases, specify the service binding name.
const string serviceOneName = "sampleMySqlServiceOne";
const string serviceTwoName = "sampleMySqlServiceTwo";
// Steeltoe: optionally change the MySQL connection strings at runtime.
builder.Services.Configure<MySqlOptions>(serviceOneName, options => options.ConnectionString += ";Use Compression=false");
builder.Services.Configure<MySqlOptions>(serviceTwoName, options => options.ConnectionString += ";Use Compression=true");
// Steeltoe: Setup DbContext connection strings, optionally changing MySQL options at runtime.
builder.Services.AddDbContext<AppDbContext>((serviceProvider, options) => options.UseMySql(serviceProvider, serviceOneName, null, untypedOptions =>
{
var mySqlOptions = (MySqlDbContextOptionsBuilder)untypedOptions;
mySqlOptions.CommandTimeout(20);
}));
builder.Services.AddDbContext<OtherDbContext>((serviceProvider, options) => options.UseMySql(serviceProvider, serviceTwoName, null, untypedOptions =>
{
var mySqlOptions = (MySqlDbContextOptionsBuilder)untypedOptions;
mySqlOptions.CommandTimeout(25);
}));
}
else
{
// Steeltoe: optionally change the MySQL connection string at runtime.
builder.Services.Configure<MySqlOptions>(options => options.ConnectionString += ";Use Compression=false");
// Steeltoe: Setup DbContext connection string, optionally changing MySQL options at runtime.
builder.Services.AddDbContext<AppDbContext>((serviceProvider, options) => options.UseMySql(serviceProvider, null, null, untypedOptions =>
{
var mySqlOptions = (MySqlDbContextOptionsBuilder)untypedOptions;
mySqlOptions.CommandTimeout(15);
}));
}
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
// Steeltoe: Insert some rows into MySQL table.
await MySqlSeeder.CreateSampleDataAsync(app.Services);
if (useMultipleDatabases)
{
await MySqlSeeder.CreateOtherSampleDataAsync(app.Services);
}
app.Run();