I was attempting to wrap a call to a service as a function in order to pass it to a retry framework. I first tried the following:
Func<Task<Obj>> serviceCall = async () => await this.client.call();
Obj callResponse = await retryPolicy.ExecuteAsync(serviceCall);
This code compiles, however, I wanted to write to log for every function call, so I attempted to add the log to the function. My thinking was to change the function to this:
Func<Task<Obj>> serviceCall = async () => {
logger.log("Calling client");
await this.client.call();
};
However, now visual studio tells me not all code paths return a value in lambda expression of type Func<Task\<Obj>>
. Somehow adding the curly braces, and turning it from an expression lambda to a statement lambda is causing some issue, I assume with the asynchronous part of the code.
CodePudding user response:
You are declaring Func<Task<Obj>>
which takes no parameters and returns a Task<Obj>
When you use the expression syntax like this:
Func<Task<Obj>> serviceCall = async () => await this.client.call();
It translates to:
Func<Task<Obj>> serviceCall = async () => { return await this.client.call(); }
So the return statement is implicit. When you use the brackets you need to specify return explicitly:
Func<Task<Obj>> serviceCall = async () => {
logger.log("Calling client");
return await this.client.call();
};