I’ve been trying something like this on a ro sdk service:
[ServiceMethod]
public async Task GetTargetIncubatorTemperature()
{
var temp1 = GetTempAsync();
var temp2 = GetTemp2Async();
var t1 = await temp1;
var t2 = await temp2;
return t2 - t1;
}
This builds, but when I try to run this server I get an exception:
System.NotSupportedException: ‘Cannot publish method InstrumentServerService.GetTargetIncubatorTemperature
Argument type not supported: System.Threading.Tasks.Task`1[[System.Double, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]’
I know we are trying to do something strange here, but this would really help speed up our service if this would work.
Is there a way to get an async await method with a return value to work server side, without using events?
As a workaround please try the following approach:
Move the async code into a separate method and then call it using Task.Run(...).GetAwaiter().GetResult()
This approach would change method
[ServiceMethod]
public async Task<string> DoSomethingAsync(string someValue)
{
using var client = new WebClient();
var data = await client.DownloadStringTaskAsync("http://www.exa11mple.com");
return data.ToString();
}
to
public string DoSomething(string someValue)
{
var result = Task.Run(() => GetHttpAsync(someValue)).GetAwaiter().GetResult();
return result.ToUpper();
}
private async Task<string> GetHttpAsync(string value)
{
using var client = new WebClient();
return await client.DownloadStringTaskAsync("http://www.exa11mple.com");
}