-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMediatorsService.cs
51 lines (38 loc) · 1.67 KB
/
MediatorsService.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
using IMediator = MassTransit.Mediator.IMediator;
using MassTransit;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using MediatorsBenchmark.MassTransit;
using MediatorsBenchmark.MediatR;
namespace MediatorsBenchmark.Services;
public class MediatorsService
{
private static readonly IRequestClient<MassTransitRequest> _massTransitMediator;
private static readonly ISender _mediatRMediator;
static MediatorsService()
{
IServiceCollection services = new ServiceCollection();
// MassTransit
services
.AddMediator(cfg => cfg.AddConsumersFromNamespaceContaining<MassTransitConsumer>());
ServiceProvider serviceProvider = services.BuildServiceProvider();
IMediator mediator = serviceProvider.GetRequiredService<IMediator>();
_massTransitMediator = mediator.CreateRequestClient<MassTransitRequest>();
// MediatR
services
.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
serviceProvider = services.BuildServiceProvider();
_mediatRMediator = serviceProvider.GetRequiredService<ISender>();
}
public static MassTransitResponse MassTransitMediator(MassTransitRequest request)
{
return _massTransitMediator.GetResponse<MassTransitResponse>(request).Result.Message;
}
public static MediatRResponse MediatRMediator(MediatRRequest request)
{
return _mediatRMediator.Send(request).Result;
}
public static DirectResponse Direct(DirectRequest directRequest) => new(directRequest.Message);
}
public sealed record DirectRequest(string Message);
public sealed record DirectResponse(string Message);