-
-
Save gztamas/50997c72c61b9ed7809156bdb76c7be1 to your computer and use it in GitHub Desktop.
Frame chain is being re-arranged, tried to set Lamar.IoC.Instances.InstanceConstructorFrame as the 'Next
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Wolverine; | |
var builder = WebApplication.CreateBuilder(args); | |
var s = new SemaphoreSlim(1); | |
builder.Host.UseWolverine(opt => | |
{ | |
opt.LocalQueue(Queues.External).Sequential().MaximumParallelMessages(1); | |
opt.PublishMessage<ExternalCommand>().ToLocalQueue(Queues.External); | |
opt.PublishMessage<ThingHappened>().ToLocalQueue(Queues.Events); | |
opt.PublishMessage<InternalCommand>().ToLocalQueue(Queues.Internal); | |
}); | |
var app = builder.Build(); | |
app.MapGet("/", () => "Hello World!"); | |
app.MapGet("/do", async (ICommandBus bus, CancellationToken cancellationToken) => | |
{ | |
var commands = Enumerable.Range(1, 100).Select<int, object>(i => i % 2 == 0 ? new ExternalCommand(i) : new ExternalCommand2(i)); | |
var results = commands.AsParallel().Select<object, Task<int>>(async c => await ProcessCommand(c, bus, cancellationToken)) | |
.Select(t => t.Result).ToList(); | |
return Results.Ok(results); | |
}); | |
app.Run(); | |
async Task<int> ProcessCommand(object command, ICommandBus commandBus, CancellationToken cancellationToken) | |
{ | |
// await s.WaitAsync(); | |
// try | |
// { | |
// return await commandBus.InvokeAsync<int>(command, cancellationToken); | |
// } | |
// finally | |
// { | |
// s.Release(); | |
// } | |
if (command is ExternalCommand ec) | |
return await commandBus.InvokeAsync<int>(ec, cancellationToken); | |
else if (command is ExternalCommand2 ec2) | |
return await commandBus.InvokeAsync<int>(ec2, cancellationToken); | |
Console.WriteLine("Weird"); | |
return 0; | |
} | |
public class ExternalCommandHandler | |
{ | |
private readonly ICommandBus commandBus; | |
public ExternalCommandHandler(ICommandBus commandBus) | |
{ | |
this.commandBus = commandBus; | |
} | |
public async Task<int> Handle(ExternalCommand cmd, CancellationToken cancellationToken) | |
{ | |
var e = new ThingHappened(cmd.Id); | |
Console.WriteLine($"Handling External Command {cmd.Id}"); | |
return await commandBus.InvokeAsync<int>(e, cancellationToken); | |
} | |
} | |
public class ExternalCommand2Handler | |
{ | |
private readonly ICommandBus commandBus; | |
public ExternalCommand2Handler(ICommandBus commandBus) | |
{ | |
this.commandBus = commandBus; | |
} | |
public async Task<int> Handle(ExternalCommand2 cmd, CancellationToken cancellationToken) | |
{ | |
var e = new ThingHappened(cmd.Id); | |
Console.WriteLine($"Handling External Command {cmd.Id}"); | |
return await commandBus.InvokeAsync<int>(e, cancellationToken); | |
} | |
} | |
public class DomainEventHandler | |
{ | |
private readonly ICommandBus commandBus; | |
public DomainEventHandler(ICommandBus commandBus) | |
{ | |
this.commandBus = commandBus; | |
} | |
public async Task<int> Handle(ThingHappened ev, CancellationToken cancellationToken) | |
{ | |
var cmd = new InternalCommand(ev.Id); | |
Console.WriteLine($"Handling Domain Event {cmd.Id}"); | |
return await commandBus.InvokeAsync<int>(cmd, cancellationToken); | |
} | |
} | |
public class InternalCommandHandler | |
{ | |
private static Random r = new(); | |
public async Task<int> Handle(InternalCommand cmd, CancellationToken cancellationToken) | |
{ | |
await Task.Delay(r.Next(1, 3), cancellationToken); | |
cancellationToken.ThrowIfCancellationRequested(); | |
Console.WriteLine($"Handling Internal Command {cmd.Id}"); | |
return r.Next(); | |
} | |
} | |
public sealed record ExternalCommand(int Id); | |
public sealed record ExternalCommand2(int Id); | |
public sealed record ThingHappened(int Id); | |
public sealed record InternalCommand(int Id); | |
public static class Queues | |
{ | |
public const string External = "ext"; | |
public const string Internal = "int"; | |
public const string Events = "evt"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment