Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active July 10, 2025 21:16
Show Gist options
  • Select an option

  • Save dcomartin/e17a3b5c31fec334f8fa9f71da976c33 to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/e17a3b5c31fec334f8fa9f71da976c33 to your computer and use it in GitHub Desktop.
public class SendMessageController : Controller
{
private readonly MyDataContext _dataContext;
public SendMessageController(MyDataContext dataContext)
{
_dataContext = dataContext;
}
[HttpGet("/")]
public async Task<string> Get([FromServices] IMessageSession session)
{
var id = Guid.NewGuid().ToString();
await _dataContext.MyEntities.AddAsync(new MyEntity { Id = id, Processed = false });
await _dataContext.SaveChangesAsync();
var message = new MyMessage { EntityId = id };
await session.SendLocal(message);
return $"Message with entity ID '{id}' sent to endpoint";
}
}
public class MyHandler : IHandleMessages<MyMessage>
{
private static readonly ILog Log = LogManager.GetLogger<MyHandler>();
private readonly MyDataContext _dataContext;
public MyHandler(MyDataContext dataContext)
{
_dataContext = dataContext;
}
public async Task Handle(MyMessage message, IMessageHandlerContext context)
{
Log.Info("Message received at endpoint");
var entity = await _dataContext.MyEntities
.Where(e => e.Id == message.EntityId)
.FirstAsync(cancellationToken: context.CancellationToken);
entity.Processed = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment