【发布时间】:2014-03-03 20:15:01
【问题描述】:
我有一个看起来像这样的控制器方法:
[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调用方法时我该怎么做呢? - 更改以便我的调用与同步上下文一起工作
当谈到这两种解决方案时,我都迷失了。有人可以帮忙吗?
【问题讨论】:
-
问题似乎不是来自于调用委托,而是来自于你正在对任务进行阻塞等待,而此时你应该异步等待。
标签: c# async-await asp.net-web-api2