I'm extending an existing Rpc library to support async methods.
In the current implementation, I'm using factory methods to build typed delegates, like this (I'm leaving out some implementation details, unrelated to the question):
public static Func<ServiceType, PayloadType, object> BuildServiceMethodInvocation<ServiceType>(MethodInfo serviceMethod)
{
return (service, payload) =>
{
try
{
// payload is mapped onto an object[]
var parameters = ReadPayload(payload)
var result = serviceMethod.Invoke(service, parameters);
return result;
}
catch (TargetInvocationException e)
{
// bla bla bla handle the error
throw;
}
};
}
The resulting object
is then passed on to a serialization class, which will handle every case.
Now I want to support also asynchronous methods, that is, methods that return a Task<T>
.
I want to change the signature of BuildServiceMethodInvocation
so that it's a Func<ServiceType, PayloadType, Task<object>>
.
I don't know how to easily wrap every possible value in a Task<object>
: it should handle plain values, but also Task<T>
(boxing into an object).
Any ideas?
Edit:
serviceMethod could return a string, and I want to get a Task<object>
returning the string value.
But serviceMethod could return a Task<int>
, in which case I would like to get back a Task<object>
returning the int value (boxed into an object).