问题描述
我完全理解HTTP世界不是单向通话的最佳选择和的WebAPI设计最适合HTTP通信冗长。毫无疑问,WCF是这里的赢家。但是,如果你已经拥有了一堆你需要有一个单一的单向通话过一些接触点动词和ApiController?而你不希望托管/为维护其他服务(WCF)。
I totally understand that HTTP world is not the best choice for one-way calls and that WebApi is designed best for HTTP verbose communications. No doubt, WCF is the winner here. But, what if you already have an ApiController with a bunch of verbs exposed and at some point you needed to have a single one-way call too? And you don't want to host/maintain another service (WCF) for that.
Task<HttpResponseMessage> response = client.PostAsJsonAsync<Log>("api/log", log)
如果你不处理的响应,那么你有类似的东西发射后不管。这是的WebAPI的唯一方法或有其他解决方案?
If you don't handle the response then you've got something similar to fire-and-forget. Is this the only way in WebApi or there's another solution?
推荐答案
您最好的选择是开始一个新的任务并立即发送响应的而不是的返回任务
Your best bet is to start a new Task and send the response immediately rather than returning the task:
public void PostDoFireAndForget() // and NOT public Task PostDoFireAndForget()
{
Task.Factory.StartNew
(() =>
{
// ... start the operation here
// make sure you have an exception handler!!
}
);
}
这篇关于消防忘记和单向的ASP.NET的WebAPI调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!