Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlushOutGoingMessagesAsync() called before After() Middleware, causing messages not to be sent #1083

Closed
JakeStevenson opened this issue Oct 16, 2024 · 0 comments · Fixed by #1085
Milestone

Comments

@JakeStevenson
Copy link

Describe the bug
When applying After() middleware that utilizes the messagebus, messages are not sent.

To Reproduce
Sample Code:

// This works when directly injected into my wolverine asp.net handlers, and clients receive the message
    public class InformAllClients
    {
        private IMessageBus _bus;
        public InformAllClients(IMessageBus bus)
        {
            _bus = bus;
        }
        public async Task Inform(MyAppMessage message)
        {
            await _bus.SendAsync(new UpdateProcessedNotification() { CommandName= message.GetType().Name });
        }
    }

// This does get called when registered as middleware, but clients do not receive the message
// options.Policies.ForMessagesOfType<MyAppMessage>().AddMiddleware(typeof(InformAllClientsMiddleware));
    public static class InformAllClientsMiddleware
    {
        public static async Task After(MyAppMessage message, IMessageBus bus)
        {
            System.Diagnostics.Debug.WriteLine($"Informing {message.GetType().Name}");
            await bus.SendAsync(new UpdateProcessedNotification() { CommandName= message.GetType().Name });
        }
    }

When examining the generated code, we can see this:

        public override async System.Threading.Tasks.Task Handle(Microsoft.AspNetCore.Http.HttpContext httpContext)
        {
            // .... code that does the work
            // Have to flush outgoing messages just in case Marten did nothing because of https://github.com/JasperFx/wolverine/issues/536
            await messageContext.FlushOutgoingMessagesAsync().ConfigureAwait(false);

            await CompounderPlus.Application.Services.InformAllClientsMiddleware.After(command, messageContext).ConfigureAwait(false);
            // Wolverine automatically sets the status code to 204 for empty responses
            if (!httpContext.Response.HasStarted) httpContext.Response.StatusCode = 204;
        }
    }

It appears that calling FlushOutgoingMessagesAsync() before it calls the middleware prevents the messages from being sent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants