-1

I am trying to create a middleware that should execute after the execution of endpoint. But when I add middleware in after the app.UseEndPoints(), it never gets invoked, however it is invoked if placed before app.UseEndPoints(), but that invokes it before execution of endpoint.

I tried to create a middleware, I placed if after app.UseEndPoints() middleware in the Configure method in the Startup class and expected it to be invoked after the execution of the endpoint (Web APIs action method), but it did not get invoked.

2
  • 2
    EndPoints is the key word here. Those are terminal middlewares, nothing gets passed on after them. You need to add the middleware before the endpoints and handle whatever it is as the response is being sent after your endpoints execute (the // Logic After in Shahar's answer). Commented Feb 7, 2024 at 17:40
  • This smells like an X-Y problem. What are you actually trying to do? Commented Feb 7, 2024 at 18:27

1 Answer 1

3

How is your middleware looks like? looks like a code smell (maybe divide it into two middlewares).

In the meantime, you can easily divide the logic into two sections:

public async Task InvokeAsync(HttpContext context)
{
    // Logic Before

    await _next(context);

    // Logic After
}

Then the // Logic After will be exectute after the invocation UseEndpoints

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.