-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathProgram.cs
More file actions
135 lines (114 loc) · 4.48 KB
/
Program.cs
File metadata and controls
135 lines (114 loc) · 4.48 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using DispatchR.Extensions;
using Sample;
using Scalar.AspNetCore;
using DispatchRNotificationSample = Sample.DispatchR.Notification;
using DispatchRSample = Sample.DispatchR.SendRequest;
using DispatchRStreamSample = Sample.DispatchR.StreamRequest;
using MediatRNotificationSample = Sample.MediatR.Notification;
using MediatRSample = Sample.MediatR.SendRequest;
using MediatRStreamSample = Sample.MediatR.StreamRequest;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddMediatR(cfg =>
{
cfg.Lifetime = ServiceLifetime.Scoped;
cfg.RegisterServicesFromAssemblies(typeof(MediatRSample.Ping).Assembly);
});
builder.Services.AddTransient(typeof(MediatR.IPipelineBehavior<,>), typeof(MediatRSample.GenericPipelineBehavior<,>));
builder.Services.AddTransient<MediatR.IPipelineBehavior<MediatRSample.Ping, int>, MediatRSample.FirstPipelineBehavior>();
builder.Services.AddTransient<MediatR.IPipelineBehavior<MediatRSample.Ping, int>, MediatRSample.SecondPipelineBehavior>();
builder.Services.AddTransient<MediatR.IStreamPipelineBehavior<MediatRStreamSample.CounterStreamRequest, string>, MediatRStreamSample.CounterPipelineStreamHandler>();
builder.Services.AddDispatchR(options =>
{
options.Assemblies.Add(typeof(DispatchRSample.Ping).Assembly);
options.RegisterPipelines = true;
options.RegisterNotifications = true;
options.PipelineOrder =
[
typeof(DispatchRSample.FirstPipelineBehavior),
typeof(DispatchRSample.SecondPipelineBehavior),
typeof(DispatchRSample.GenericPipelineBehavior<,>)
];
options.IncludeHandlers = null;
options.ExcludeHandlers = null;
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.MapGet("/Send/MediatR", (MediatR.IMediator mediatR, CancellationToken cancellationToken)
=> mediatR.Send(new MediatRSample.Ping(), cancellationToken))
.WithName("SendInMediatRWithPipeline");
app.MapGet("/Send/DispatchR", (DispatchR.Requests.IMediator dispatchR, CancellationToken cancellationToken)
=> dispatchR.Send(new DispatchRSample.Ping(), cancellationToken))
.WithName("SendInDispatchRWithPipeline");
app.MapGet("/Stream/MediatR", async (MediatR.IMediator mediatR, ILogger<Program> logger) =>
{
CancellationTokenSource cts = new();
int count = 0;
await foreach (var item in mediatR.CreateStream(new MediatRStreamSample.CounterStreamRequest(), cts.Token))
{
count++;
if (item.Contains("CodeWithHSN"))
{
cts.Cancel();
}
logger.LogInformation($"stream count in MediatR: {count}");
}
return "It works";
});
app.MapGet("/Stream/DispatchR", async (DispatchR.Requests.IMediator dispatchR, ILogger<Program> logger) =>
{
CancellationTokenSource cts = new();
int count = 0;
await foreach (var item in dispatchR.CreateStream(new DispatchRStreamSample.CounterStreamRequest(), cts.Token))
{
count++;
if (item.Contains("CodeWithHSN"))
{
cts.Cancel();
}
logger.LogInformation($"stream count in DispatchR: {count}");
}
return "It works";
});
app.MapGet("/Notification/MediatR", async (MediatR.IMediator mediator, ILogger<Program> logger) =>
{
await mediator.Publish(new MediatRNotificationSample.MultiHandlersNotification(Guid.Empty));
return "It works";
});
app.MapGet("/Notification/DispatchR", async (DispatchR.Requests.IMediator mediator, ILogger<Program> logger) =>
{
await mediator.Publish(new DispatchRNotificationSample.MultiHandlersNotification(Guid.Empty), CancellationToken.None);
return "It works";
});
app.Run();
namespace Sample
{
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}