forked from dotnet/docs-aspire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
79 lines (65 loc) · 2.22 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
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<ServiceHubContextFactory>();
builder.Services.AddProblemDetails();
builder.Services.AddOpenApi();
var isServerlessMode = builder.Configuration.GetValue<bool>("IS_SERVERLESS");
if (!isServerlessMode)
{
builder.Services.AddSignalR()
.AddNamedAzureSignalR("signalr");
}
else
{
builder.Services.AddSingleton(sp =>
{
return new ServiceManagerBuilder()
.WithOptions(options =>
{
options.ConnectionString = builder.Configuration.GetConnectionString("signalr");
})
.WithLoggerFactory(sp.GetRequiredService<ILoggerFactory>())
.BuildServiceManager();
});
}
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference(_ => _.Servers = []);
}
app.UseExceptionHandler();
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
if (isServerlessMode)
{
app.MapPost("/chathub/negotiate", async (string? userId, ServiceHubContextFactory factory) =>
{
var hubContext = await factory.GetOrCreateHubContextAsync("chathub", CancellationToken.None);
NegotiationResponse negotiateResponse = await hubContext.NegotiateAsync(new()
{
UserId = userId ?? "user-1",
});
return Results.Json(negotiateResponse, new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
});
});
// try in the command line `CURL -X POST https://localhost:53282/broadcast` to broadcast messages to the clients
app.MapPost("/chathub/broadcast", async (ServiceHubContextFactory factory) =>
{
var hubContext = await factory.GetOrCreateHubContextAsync("chathub", CancellationToken.None);
await hubContext.Clients.All.SendAsync(
HubEventNames.MessageReceived,
new UserMessage("server", "Started..."));
});
}
else
{
app.MapHub<ChatHub>(HubEndpoints.ChatHub);
}
app.MapDefaultEndpoints();
app.Run();