我有一个MessageHandler,我需要以不同的间隔将2条消息发送到同一处理程序。我的代码仅将第二条消息发送给BUS。预期结果是第一条消息将在1分钟后发送,第二条消息将在3分钟(2 + 1)之后发送。
public async Task Handle(EmailMessage message, IMessageHandlerContext context)
{
switch (message.Action)
{
case Constants.Ticket.TicketConstants.Action.SendBothMails:
DeferNextMessage(message, context);
break;
case Constants.Ticket.TicketConstants.Action.SendFirstMail:
//logic for the first email
var a = "firstMail";
break;
case Constants.Ticket.TicketConstants.Action.SendSecondMail:
//logic for the second email
var b = "secondMail";
break;
}
}
private async void DeferNextMessage(EmailMessage currentMessage, IMessageHandlerContext context)
{
await Test1(currentMessage, context).ContinueWith(async t => {
await Test2(currentMessage, context);
});
}
private async Task Test1(EmailMessage currentMessage, IMessageHandlerContext context)
{
var sendFirstMailOptions = new SendOptions();
sendFirstMailOptions.DelayDeliveryWith(TimeSpan.FromMinutes(2));
sendFirstMailOptions.RouteToThisEndpoint();
await context.Send(currentMessage.SpawnFirstEmailMessage(), sendFirstMailOptions).ConfigureAwait(false);
}
private async Task Test2(EmailMessage currentMessage, IMessageHandlerContext context)
{
var sendSecondMailOptions = new SendOptions();
sendSecondMailOptions.DelayDeliveryWith(TimeSpan.FromMinutes(2));
sendSecondMailOptions.RouteToThisEndpoint();
await context.Send(currentMessage.SpawnSecondEmailMessage(), sendSecondMailOptions).ConfigureAwait(false);
}