简体   繁体   English

ASP.NET MVC同步与异步WCF调用

[英]ASP.NET MVC Synchronous vs Asynchronous WCF calls

I working on an ASP.NET MVC which is loading its data completely via a WCF service. 我正在使用ASP.NET MVC,它通过WCF服务完全加载其数据。 What my model does is just call the WCF service. 我的模型所做的只是调用WCF服务。 The controller then passes the model to the view. 然后,控制器将模型传递到视图。 I read in this article that it is always better to do async calls if you can. 我在看这个文章,它始终是更好地做异步调用,如果你能。 However I am still not sure which call to use. 但是我仍然不确定使用哪个电话。 What would happen if the data form the service takes a while to load? 如果服务中的数据加载需要一段时间,该怎么办? Does it mean that my controller code will keep running and if the data is not there when the return statement is reached my controller will return an empty view, which could also cause a null pointer exception because of the view accessing unpopulated model fields? 这是否意味着我的控制器代码将继续运行,并且在到达return语句时如果数据不存在,则我的控制器将返回一个空视图,这也可能导致空指针异常,因为该视图访问未填充的模型字段?

If your controller returns a Task then you will be fine. 如果您的控制器返回任务,那么您会没事的。 If you are on C# 5.0 you can use async/await to make your code more maintainable than what is shown in the article. 如果您使用的是C#5.0,则可以使用async / await使您的代码比本文中所显示的更具可维护性。 Basically your code will look like this 基本上,您的代码将如下所示

public async Task<ActionResult> SomeAction()
{
    var someData = await wcfServiceProxy.GetDataAsync() //alternatively an async call to your model service which should also be async
    //some work with the data here
    return View(someData);
}

or you can just not care and use all synchronous calls. 或者您根本可以不在乎并使用所有同步呼叫。 The downside of using synchronous calls is that your throughput would suffer. 使用同步调用的缺点是吞吐量会受到影响。 You will be able to handle 200 requests per second instead of 50K requests per second (and I am pulling these numbers out of thin air). 您将能够每秒处理200个请求,而不是每秒处理5万个请求(而我正在凭空获取这些数字)。 Of course in most cases you simply don't care because your project never does more than 20 requests per second. 当然,在大多数情况下,您根本不在乎,因为您的项目每秒执行的请求数量永远不会超过20个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM