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

Solution 1:

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:

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.