WCF的基于任务的异步模式 [英] task based async pattern for wcf

查看:592
本文介绍了WCF的基于任务的异步模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施WCF基于任务异步模式。该方法包括在其上存储了数据执行程序和大量的处理。以及它抛出一个异常

I am implementing task based async pattern for wcf. The method includes stored procedure execution and lots of processing on the data it got. As well it throws an exception

问题是

如何实施

选项1。

*await command.ExecuteScalarAsync();
//run 10000 lines of processing including exception handling*

选项2

*await command.ExecuteScalarAsync();
Task.Factory.StartNew(() => run 10000 lines of processing including exception handling);*

可能还有其他的选择......?

may be there other options...?

利弊

另外,如果我已经实现同步该方法 - 我应该使用它

Also if I already implemented the sync for that method - should I use it?

推荐答案

选项1是更好的选择。不过可以肯定您记录了该功能既具有异步组件和长时间运行的同步组件。

Option 1 is the better option. However be sure you document that the function has both a asynchronous component and a long running synchronous component.

如果您的函数的调用者决定同步组件的时间过长和阻止其UI或类似的主叫方可以决定包装在一个单独的线程调用。强制code是在一个单独的线程就像你在选项2一样不能很好地扩展。在那里你会遇到高负载,如Web服务器的情况下,可以极大地产生这些额外的不必要的伤害线程性能。

If the caller of your function decides that the synchronous component is taking too long and blocking their UI or similar the caller can decide to wrap the call in a separate thread. Forcing the code to be in a separate thread like you did in option 2 does not scale well. In situations where you will experience high loads, like web servers, you can greatly hurt performance by generating those extra unnecessary threads.

这篇关于WCF的基于任务的异步模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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