我想创建一个与.Net核心框架运行异步函数的Web服务。在C中的API多线程#
该函数必须在其自己的线程中执行并返回一个新的值,该值将被发送给适当的调用者。 服务必须在异步函数运行时监听其他调用,并为每个调用启动一个新线程。 一旦线程完成,我希望将响应发送给原始调用者。 我已经试过这样:
在我的控制,我有:
[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
}
}
在我的主类,我有:
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;
}
正如你可以看到我想我的功能GetResultAsync返回一个字符串与将被发送给调用者的值。 但我不能有这样的事情(见下面的代码),因为GetResultAsync返回一个任务,而不是一个字符串:
public string RunOrchestration(int value)
{
return r = Program.GetResultAsync(value);
}
如果我把一个地等待着RunOrchestration,它会等待响应,并表现作为同步功能。
任何人都有一个想法,如何得到我的回应,并将其返回给适当的来电者?
提前致谢! :)
阅读SignalR – Nkosi
async!=并行执行 – Tseng
当我的异步函数正在运行时,该服务必须监听其他调用,并为每个调用启动一个新线程 - >您确实知道调用是并发处理的除非你故意禁用了这个功能?那么为什么这种结构? –