我有一個看起來像這樣的控制器方法調用的任務:等待在委託
[HttpPut, Route("cqs/command")]
public HttpResponseMessage Command([ValueProvider(typeof(HeaderValueProviderFactory))] string typeName)
{
object reply = null;
var code = HttpStatusCode.OK;
try
{
var cmdObject = DeserializeRequest(typeName);
var method = _commandMethod.MakeGenericMethod(type);
method.Invoke(this, new object[] { request });
}
catch (Exception exception)
{
code = HttpStatusCode.InternalServerError;
reply = exception;
}
var responseMsg = new HttpResponseMessage(code);
if (reply != null)
{
responseMsg.Headers.Add("X-TypeName", reply.GetType().AssemblyQualifiedName);
var replyJson = JsonConvert.SerializeObject(reply);
responseMsg.Content = new StringContent(replyJson, Encoding.UTF8, "application/json");
}
return responseMsg;
}
它調用下面的方法:
private void ExecuteCommand<T>(T command) where T : Command
{
var task = _commandBus.ExecuteAsync(command);
task.Wait(TimeSpan.FromSeconds(10));
}
的理由是,該_commandBus
只有一個我需要調用的通用方法。
然而,問題在於ExecuteCommand
在某些時候似乎陷入僵局。我無法弄清楚爲什麼。 ICommandBus.ExecuteAsync
方法將使用async/await調用其他任務,因此WebApi使用同步上下文可能會導致某種死鎖。 (await vs Task.Wait - Deadlock?)
所以,如果我理解正確的這一切,可能有兩個解決方案:
- 使用異步/等待所有的方式。但是,如何在使用
MethodInfo
調用方法時執行此操作? - 變化,使我的調用都與當它涉及到這兩種解決方案,我失去了同步上下文
。誰能幫忙?
該問題似乎並不來自調用委託,儘管它來自於您正在執行阻止等待任務的事實,當你應該異步等待。 – Servy