jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
10.84k stars 1.15k forks source link

Issues with MediatR on .Net 8 and AWS Lambda #994

Closed dguisinger closed 5 months ago

dguisinger commented 5 months ago

I am having trouble getting MediatR to work in AWS Lambda under .NET 8 and Amazon.Lambda.AspNetCore hosting.

A quick summary, I am using API Gateway->Lambda via a {proxy+} url configuration, which proxies HTTP requests into the Lambda. The Amazon.Lambda.AspNetCore hosting replaces Kestrel. I am using AspNet.Core to get the built in authentication middleware and to make use of Swagger UI.

From a code perspective, it looks like an ordinary AspNet Core WebAPI project.

So far so good, everything works properly running on localhost (on local host, it uses Kestrel).

When I upload into lambda, it keeps locking up. Inserting a bunch of Console.WriteLine statements through out my code, I can trace it through my behaviors and into my handler, to where log messages just suddenly stop (and random places). API Gateway then times out at (30 seconds?), and the lambda instance keeps running up to its configured timeout (15 minutes) without any additional logging until it is killed by AWS. The code locally runs through to completion in under 100ms.

It feels like some sort of an async issue, but I can't seem to narrow it down. I've tried removing all behaviors to no effect. The only thing that seems to work is removing the call to MediatR Send, creating an instance of the handler class directly and calling await handler.Handle() directly.

Is MediatR doing anything under the hood that could be causing this issue? I am completely stumped at this point.

jbogard commented 5 months ago

Are you awaiting the call to _mediator.Send? All MediatR does is call into IServiceProvider to instantiate a handler, then await handler.Send. You can take a look at the underlying Mediator class, it's not doing that much.

dguisinger commented 5 months ago

I am indeed, I can't explain it...

Its likely not a MediatR issue, but I thought I'd ask. I'm guessing its something to do with AWS's .NET 8 preview container image, but the behaviors seem so random I've been looking all over the for the cause with the assumption I have to be something wrong.

Last night I managed to break Swagger UI as well, and it ended up coming from a change to AspNetCore "Information" logging... switching it back to Warning fixed Swagger.... so I then went and deleted my AddLogger calls during startup, and now everything works.... 75% of the time. My MediatR commands now either execute in a couple ms, or completely hangs. I am still completely baffled.