fast-endpoints: trigger event from anywhere
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
global using FastEndpoints; | |
var builder = WebApplication.CreateBuilder(); | |
builder.Services.AddFastEndpoints(); | |
var app = builder.Build(); | |
app.UseAuthorization(); | |
app.UseFastEndpoints(); | |
Task.Run(async () => | |
{ | |
await Task.Delay(1000); | |
await Event<OrderCreatedEvent>.PublishAsync(new() | |
{ | |
OrderID = "001", | |
CustomerName = "customer name", | |
OrderTotal = 1234.56m | |
}); | |
}); | |
app.Run(); | |
public class OrderCreatedEvent | |
{ | |
public string OrderID { get; set; } | |
public string CustomerName { get; set; } | |
public decimal OrderTotal { get; set; } | |
} | |
public class OrderCreationHandler : FastEventHandler<OrderCreatedEvent> | |
{ | |
public override Task HandleAsync(OrderCreatedEvent eventModel, CancellationToken ct) | |
{ | |
var logger = Resolve<ILogger<OrderCreationHandler>>(); | |
logger.LogInformation($"order created event received:[{eventModel.OrderID}]"); | |
return Task.CompletedTask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment