
I am trying to add record through a webhook, it works on my local but on production it gives me 500 internal server error.

I am trying to add record through a webhook, it works on my local but on production it gives me 500 internal server error.
Best answer by aleksejslusar19
Hi
Webhook Handler Works Locally but Fails Silently in Production? Check for Async Violations
I recently ran into an issue where a webhook handler worked flawlessly in my local environment, but silently failed in production — no logs, no exceptions, no status codes other than a silent timeout.
After some digging, the root cause was simple — but easy to overlook:
A synchronous method call inside an async handler.
---
❌ Problem Code
Here's the relevant piece of code I had inside the `IWebhookHandler.HandleAsync` method:
var res = SaveRequestResult(result); // ⚠ Sync call inside async methodEven though the method `SaveRequestResult()` didn’t do anything obviously blocking, calling it like this violated async execution flow, which seems to be tolerated locally but not in production (due to hosting environment or synchronization context).
---
✅ Fixed Version
The fix was straightforward — wrap the call in `Task.Run(...)` and `await` it:
var res = await Task.Run(() => SaveRequestResult(result));After this change, the webhook started working reliably in production.
---
🧠 Key Takeaway
Even internal method calls inside an async handler must not block. If you're not using `await`, you're probably doing it wrong. Treat your webhook handler as fully async, including all operations inside it — especially if it's processing IO, parsing payloads, or writing data.
---
💬 Real-World Example
public async Task HandleAsync(WebhookContext context, CancellationToken cancellation)
{
var requestUri = new WWWebhookContentHelper(context).GetRequestUri();
using (var scope = GetAdminScope())
{
if (_authenticator == null || !_authenticator.Authenticate(requestUri))
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
return;
}
using (var result = new JsonTextReader(context.Request.CreateTextReader()))
{
// ✅ Async-safe method call
var res = await Task.Run(() => SaveRequestResult(result));
context.Response.StatusCode = res
? StatusCodes.Status202Accepted
: StatusCodes.Status406NotAcceptable;
}
}
}---
Hope this helps you avoid hours of silent debugging.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.