Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (81 loc) · 4.84 KB
/
Copy pathProgram.cs
File metadata and controls
98 lines (81 loc) · 4.84 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
namespace Contoso.Customers.Api;
public class Program
{
private static void Main(string[] args)
{
// Create the web builder.
var builder = WebApplication.CreateBuilder(args);
// Add CoreEx host settings.
builder.AddHostSettings();
builder.Services.Configure<OpenTelemetry.Logs.OpenTelemetryLoggerOptions>(o => o.IncludeScopes = true);
// Add CoreEx services.
builder.Services
.AddPrecisionTimeProvider()
.AddExecutionContext()
.AddReferenceDataOrchestrator()
.AddMvcWebApi()
.AddHttpWebApi();
// Add all the dynamically registered services.
builder.Services.AddDynamicServicesUsing<ReferenceDataProvider, CustomerRepository>();
// Add L1/L2 caching services and the Redis backplane.
builder.Services.AddMemoryCache();
builder.AddRedisDistributedCache("redis");
builder.Services.AddFusionCache()
.WithRegisteredMemoryCache()
.WithRegisteredDistributedCache()
.WithBackplane(sp => new RedisBackplane(new RedisBackplaneOptions { Configuration = sp.GetRequiredService<IOptions<ConfigurationOptions>>().Value.ToString() }))
.WithSystemTextJsonSerializer(JsonDefaults.SerializerOptions);
builder.Services
.AddFusionHybridCache() // Adds the CoreEx.Caching.IHybridCache for FusionCache.
.AddDefaultCacheKeyProvider() // Adds the default CoreEx.Caching.ICacheKeyProvider.
.AddRedisCoordination() // Adds Redis-backed synchronization and idempotency storage.
.AddIdempotencyProvider(); // Adds the ASP.NET Core idempotency integration.
// Add the Cosmos DB client (Aspire) - in Development, configured to accept the local emulator's self-signed certificate and use Gateway mode, matching CosmosTestBase's own approach.
builder.AddAzureCosmosClient("Cosmos", configureClientOptions: o =>
{
o.UseSystemTextJsonSerializerWithOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
if (builder.Environment.IsDevelopment())
{
o.ConnectionMode = ConnectionMode.Gateway;
o.HttpClientFactory = () => new HttpClient(new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator });
}
});
// Add the Cosmos repository and related outbox services.
builder.Services.AddCosmosDb<CustomersCosmosDb>("contoso");
builder.Services
.AddEventFormatter() // Adds the EventFormatter to enable message formatting for publishing.
.AddNamedDestinationProvider() // Adds the NamedDestinationProvider; events to the shared topic, commands to per-domain queues.
.AddCosmosDbEventPublisher() // Adds the CosmosDbEventPublisher/IEventPublisher
.AddCosmosDbUnitOfWork() // Adds the CosmosDbUnitOfWork/IUnitOfWork, matching AddPostgresUnitOfWork/AddSqlServerUnitOfWork's multi-register shape.
.AddCosmosDbHealthCheck(); // Adds the CosmosDbHealthCheck - Aspire's own AddAzureCosmosClient does not register one itself, unlike its Npgsql/SqlClient counterparts.
// Post-configure all health-checks; adds the standard tags.
builder.Services.PostConfigureAllHealthChecks();
// Add the ASP.NET Core services.
builder.Services.AddControllers();
// Add the OpenAPI services.
builder.Services.AddOpenApiDocument(s =>
{
s.Title = builder.Environment.ApplicationName;
s.AddCoreExConfiguration();
});
// Add OpenTelemetry tracing.
builder.WithCoreExTelemetry()
.WithCoreExCosmosDbTelemetry()
.UseOtlpExporter();
// Build the application.
var app = builder.Build();
// Configure the pipeline/middleware (order is important).
app.UseCoreExExceptionHandler();
app.UseExecutionContext();
app.UseHttpsRedirection();
// app.UseAuthentication(); // TODO: register an authentication scheme (builder.Services.AddAuthentication(...)) then uncomment.
app.UseAuthorization();
app.UseIdempotencyKey();
app.MapControllers();
app.UseOpenApi();
app.UseSwaggerUi();
app.MapHealthChecks(); // Secure by default: detailed endpoints are disabled unless explicitly enabled (HealthCheckOptions.AreDetailedEndpointsEnabled) and secured (detailedGroupConfigure, e.g. g => g.RequireAuthorization()); basic live/startup/ready checks stay anonymous for orchestrator probes.
// Run the application.
app.Run();
}
}