Let me start from some code. I have an asynchronous data channel :
interface IChannel
{
Task<byte[]> SendRecv(string command, byte[] request);
}
and synchronous interface that describe operations that can be performed on a remote server:
interface IRemoteServer
{
int DoLongTask(int param);
}
along with its implementation that uses the asynchronous data channel:
class RemoteServer : IRemoteServer
{
private IChannel _channel;
public int DoLongTask(int param)
{
var request = BitConverter.GetBytes(param);
var response = _channel.SendRecv(nameof(DoLongTask), request).Result;
return BitConverter.ToInt32(response, 0);
}
}
and finally the application that was written asynchronously and uses the remote server abstraction:
class Application
{
private IRemoteServer _server;
async Task<int> SomeMethod(int param)
{
return await Task.Run(() => _server.DoLongTask(param));
}
}
The problem of above code is that despite both the channel and application was written asynchronously, it blocks the thread-pool thread when accessing the Result
in remote server implementation. Let's assume that IRemoteServer
is immutable because its used in other places and I don't have a direct control how it looks. Now, when implementing the interface (class RemoteServer
) I can't use the async
word because C# assumes it's plain synchronous method - on the other hand I know that when executing the method I am already in a task so in theory I could use async
in runtime to join both tasks (Application.SomeMethod
and IChannel.SendRecv
).
I'm looking for the solution to this problem (including low-level/advanced hacks), any help appreciated!