I'm trying to create an Web Service that runs an asynchronous function with .Net Core Framework.
This function must be executed in its own thread and return a new value that will be sent to the proper caller. The service must be listening for other calls while my async function is running, and start a new thread for each call. Once the thread is finished, I want the response to be sent to the original caller. I have tried this way :
In my controller I have :
[Route("api/orchestration")]
public class OrchController : Controller
{
[HttpGet("{value}")]
public void RunOrchestration(int value)
{
Program.GetResultAsync(value); // Notice that no value is returned but it respects the asynchronicity and the multithreading as asked
}
}
In my main class I have :
public async static Task<string> GetResultAsync(int i)
{
return await OrchestrationAsync(i);
}
public static Task<string> OrchestrationAsync(int i)
{
return Task.Run<string>(() => { return r = Orchestration(i); });
}
public static string Orchestration(Object i)
{
// This function is an orchestrator of microservices that talk asynchronously through a MOM
// Just consider that it returns the value I want in a string
return result;
}
As you can see I want my function GetResultAsync to return a string with the value that will be sent to the caller. Yet I can't have something like this (see code below) because GetResultAsync returns a Task and not a string :
public string RunOrchestration(int value)
{
return r = Program.GetResultAsync(value);
}
And if I put an await in RunOrchestration, it will wait for the response and will behave as a synchronous function.
Anyone has an idea on how to get my response and give it back to the proper caller ?
Thank's in advance ! :)