如何使用不允许异步控制器操作的CMS处理异步API调用? [英] How should I handle async API calls using a CMS that does not allow async controller actions?

查看:113
本文介绍了如何使用不允许异步控制器操作的CMS处理异步API调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了使用控制台应用程序中的主要功能外,我一直在使用async/await来弄湿我的脚,并且大多数情况下我一直在读取状态请勿阻止异步代码" .我发现自己无法遵循这个简单的规则:(

I have been getting my feet wet using async/await and most of what I have been reading states "Don't Block on Async Code" with the exception of the main function in a console application. I find myself unable to follow this simple rule :(

我的应用程序在很大程度上取决于WebAPI层.我在MVC复制中使用的客户端正在使用System.Net.Http.HttpClient上的异步方法来调用API方法.目前,我正在以两种方式处理它.在某些地方,我们在致电客户后立即得到结果:

My application depends heavily on a WebAPI layer. The client I am using in the MVC apllication is using the async methods on System.Net.Http.HttpClient to call API methods. Currently I am handling it two ways. In some places we are getting the result immediately after calling the client:

//WebAPI Client
public object GetSomething(int id)
{
    var task = _client_GetSomethingFromApiAsync(id);
    var result = task.Result;
    return result;
}

在其他使用异步的方式中,一直使用到控制器.我正在使用SiteFinity,并且无法使用异步控制器操作,因此最终出现如下情况:

In others using async all the way up to the controller. I am using SiteFinity and cannot use async controller actions so I end up with something like this:

//WebAPI Client
public Task<object> GetSomethingAsync(int id)
{
    return _client_GetSomethingFromApiAsync(id);    
}

//Domain Service
public Task<object> GetSomethingDomainServiceAsync(int id)
{
    return _service.GetSomethingAsync(id);  
}

//Controller
public JsonResult GetSomethingControllerAction(int id)
{
    var result = _domainService.GetSomethingDomainServiceAsync(viewModel.id).Result;
    return Json(new { Success = true, Payload = result });
}

根据我的研究,尽管我没有遇到任何死锁问题,而且似乎一切正常,但这两种解决方案都不理想.有人可以帮我理解我应该遵循的最佳模式并解释原因吗?

Based on my research both of these solutions are not ideal although I have not had any issues with deadlocks and everything seems to be working. Can someone please help me understand the best pattern I should follow and explain why?

谢谢!

推荐答案

我正在使用SiteFinity,并且无法使用异步控制器操作

I am using SiteFinity and cannot use async controller actions

根据SiteFinity ,它确实支持异步控制器操作,但不支持小部件中的异步操作.如果您遇到这种情况,建议您

According to SiteFinity, it does support async controller actions, just not async actions in widgets. If this is your situation, I encourage you to vote.

假设您处在这种情况下-并且不能使用async代码-那么我建议您一直使用同步代码.也就是说,将HttpClient替换为WebClient或类似的内容.我不建议使用Result,因为将来(即使有可能)将来对HttpClient的更新可能会导致死锁,即使您的代码今天没有死锁.

Assuming that you're in this situation - and cannot use async code - then I recommend that you just use synchronous code all the way. That is, replace, HttpClient with WebClient or something like that. I do not recommend using Result since it is possible (unlikely, but possible) that future updates to HttpClient may cause deadlocks even if your code does not deadlock today.

如果您不想失去已经在async中进行的投资,那么我建议使用我在

If you don't want to lose the investment you've already made in async, then I'd recommend using the "boolean argument hack" as described in my article on brownfield async.

这篇关于如何使用不允许异步控制器操作的CMS处理异步API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆