-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (91 loc) · 3.94 KB
/
Program.cs
File metadata and controls
101 lines (91 loc) · 3.94 KB
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
98
99
100
101
using System;
using System.Linq;
using DotNetWebApp.Data;
using DotNetWebApp.Data.Dapper;
using DotNetWebApp.Data.Tenancy;
using DotNetWebApp.Models;
using DotNetWebApp.Models.Generated;
using DotNetWebApp.Services;
using DotNetWebApp.Services.Views;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Radzen;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddRadzenComponents();
builder.Services.Configure<AppCustomizationOptions>(
builder.Configuration.GetSection("AppCustomization"));
builder.Services.Configure<DataSeederOptions>(
builder.Configuration.GetSection(DataSeederOptions.SectionName));
builder.Services.Configure<TenantSchemaOptions>(
builder.Configuration.GetSection("TenantSchema"));
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped(sp =>
{
var navigationManager = sp.GetRequiredService<NavigationManager>();
var handler = new HttpClientHandler();
if (builder.Environment.IsDevelopment())
{
// Accept self-signed certificates in development
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
}
return new HttpClient(handler) { BaseAddress = new Uri(navigationManager.BaseUri) };
});
builder.Services.AddScoped<IDashboardService, DashboardService>();
builder.Services.AddScoped<ISpaSectionService, SpaSectionService>();
builder.Services.AddScoped<ITenantSchemaAccessor, HeaderTenantSchemaAccessor>();
builder.Services.AddSingleton<IModelCacheKeyFactory, AppModelCacheKeyFactory>();
builder.Services.AddSingleton<IAppDictionaryService>(sp =>
{
var env = sp.GetRequiredService<IHostEnvironment>();
var yamlPath = Path.Combine(env.ContentRootPath, "app.yaml");
return new AppDictionaryService(yamlPath);
});
builder.Services.AddSingleton<IEntityMetadataService, EntityMetadataService>();
builder.Services.AddScoped<IEntityOperationService, EntityOperationService>();
builder.Services.AddScoped<IEntityApiService, EntityApiService>();
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddScoped<DbContext>(sp => sp.GetRequiredService<AppDbContext>());
builder.Services.AddScoped<DataSeeder>();
// Dapper infrastructure (read-only, shares EF connection)
builder.Services.AddScoped<IDapperQueryService, DapperQueryService>();
// View registry (singleton, loaded once at startup from app.yaml)
builder.Services.AddSingleton<IViewRegistry>(sp =>
{
var env = sp.GetRequiredService<IHostEnvironment>();
var logger = sp.GetRequiredService<ILogger<ViewRegistry>>();
var appDictionary = sp.GetRequiredService<IAppDictionaryService>();
return new ViewRegistry(logger, appDictionary, env.ContentRootPath);
});
// View service (scoped, executes views)
builder.Services.AddScoped<IViewService, ViewService>();
var seedMode = args.Any(arg => string.Equals(arg, "--seed", StringComparison.OrdinalIgnoreCase));
var app = builder.Build();
if (seedMode)
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await dbContext.Database.MigrateAsync();
await scope.ServiceProvider.GetRequiredService<DataSeeder>().SeedAsync();
return;
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();