Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression should be MethodCallExpression error calling Expression<Action>

Tags:

c#

async-await

What is the reason for the following code throwing this exception saying "Expression has to be MethodCallExpression. I thought calling an Action IS that..

     Action startBouncePolling = new Action(async () =>
    {

    });

    BackgroundJob.Enqueue(() => startBouncePolling());

Signature of Enqueue is one parameter of Expression<Action>

like image 848
parliament Avatar asked Aug 01 '26 19:08

parliament


1 Answers

When you call startBouncePolling(), you're not calling a method. startBouncePolling is not a method, it's a delegate. So you are actually invoking a delegate.

Therefore, the body of the lambda expression () => startBouncePolling() ends up being a InvocationExpression, not a MethodCallExpression.

If you use a method instead of a delegate, for example:

public async Task StartBouncePolling()
{
}
...
BackgroundJob.Enqueue(() => StartBouncePolling());

then the body would be a MethodCallExpression.

like image 194
Eren Ersönmez Avatar answered Aug 04 '26 11:08

Eren Ersönmez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!