An expression tree may not contain a call or invocation that uses optional arguments

C#asp.net Mvc-3

C# Problem Overview


> An expression tree may not contain a call or invocation that uses > optional arguments

return this.RedirectToAction<MerchantController>(x => x.Edit(merchantId));

Where edit had a second, nullable argument.

Why is this?

C# Solutions


Solution 1 - C#

Had the same message when trying to use Mock.setup to mock a method with multiple default parameters. I just had to add the additional parameters in the lambda.

void someMethod(string arg1 = "", string arg2 = "")

mockedObject.Setup(x => x.someMethod(It.IsAny<string>(), It.IsAny<string>()))

Solution 2 - C#

The underlying expression tree API does not support optional arguments.

For IL-compiled code the C# compiler inserts the default values at compile time (hard-coded), because the CLR does not support calling methods with optional arguments either when the arguments are not provided explicitly.

Solution 3 - C#

Error: 'an exception tree may not contain a call or invocation that uses option arguments'

Why: Because you are not providing the optional parameters when calling the method. Mainly you get this with .net core when using IAsyncProxy service object.

Fix: Pass all the optional parameters value, you may use default value if you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbevacquaView Question on Stackoverflow
Solution 1 - C#ds4940View Answer on Stackoverflow
Solution 2 - C#usrView Answer on Stackoverflow
Solution 3 - C#RakeshView Answer on Stackoverflow