Is it possible to have async service methods returning a task

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?

Thank you

Hello

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");
		}

Regards

Thanks, logged as bugs://85792

This is what I was looking for.
Thank you very much

I’ve logged an issue to add built-in support for async server methods for .NET Core and .NET 5/6 - based servers

1 Like

We have added native support for Task-based methods:

https://talk.remobjects.com/t/remoting-sdk-for-net-vnext-new-features/18046/33

bugs://85792 got closed with status fixed.

Great! Thanks

1 Like