如果Web应用程序中的数据访问在不同的任务中运行? [英] Should data access in Web applications run on different Tasks?

查看:157
本文介绍了如果Web应用程序中的数据访问在不同的任务中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一系列的ASP.Net的Web API服务,基本上从数据库获取数据并返回。

我们决定现在重用previous写得不好的数据访问对象(姑且称之为 PoorDAO )使用ADO.Net来调用数据库中的存储过程。

在未来的一个改进将重写该数据访问层从中受益<一href=\"http://www.itworld.com/development/405462/why-you-should-use-async-tasks-net-45-and-entity-framework-6\"相对=nofollow>异步数据呼叫与实体框架。

由于此,我们决定换行 PoorDAO 的资源库中的执行暴露异步方法的接口。该想法是保持相同的接口,用于未来的EF异步库

//未来通用接口
公共接口ICountryRepository
{
  任务&LT;国家&GT; GetAllCountries();
}//目前执行的耻辱隐藏PoorDAO
公共类CountryRepository:ICountryRepository
{
  公共任务&LT;国家&GT; GetAllCountries()
  {
    变种国家= PoorCountryDAO.GetAllcountries(); //不佳的API调用    //一些数据转换...    返回Task.FromResult(结果);
  }
}

我们在这里基本上是同步操作躲在异步服装。这一切都很好,但我的问题是:虽然我们在这,岂不是更好地使该方法完全异步和呼叫等待Task.Run(()=&GT; poorCountryDAO.GetAllcountries ()),而不是仅仅 poorCountryDAO.GetAllcountries()

据我所知,这将腾出的IIS线程在Web API服务的HTTP请求当前正在运行,并创建或重用另一个线程。该线程将被阻塞,等待数据库响应,而不是IIS线程被阻塞。的是任何更好的资源是否明智?难道我完全误解或overinter preT Task.Run()是如何工作的?

编辑:我碰到<少时href=\"http://www.tugberkugurlu.com/archive/asynchronous-database-calls-with-task-based-asynchronous-programming-model-tap-in-asp-net-mvc-4\"相对=nofollow>其中声称,在某些情况下,异步调用数据库可能会导致一个8倍的性能提升这篇文章。他的情况非常接近我的。我不能让我的头周围怎么会在这里给出的答案是可能的,我有点困惑该怎么办...


解决方案

  

是,任何更好的资源是否明智?


没有;这可证明更糟。现有的 Task.FromResult 等待是最好的解决方案。

Task.Run Task.Factory.StartNew Task.Start 不应该在ASP.NET应用程序中使用。他们窃取从的相同的线程ASP.NET使用,导致额外的线程切换池中的线程。另外,如果他们是长期运行的,他们会乱用ASP.NET的默认线程池的启发,还可能导致创建和销毁不必要的线程。

I'm writing a series of ASP.Net Web Api services that basically get data from a database and return it.

We decided for now to reuse previous poorly written Data Access Objects (let's call them PoorDAO) that use ADO.Net to call stored procedures in the database.

One improvement in the future will be to rewrite that data access layer to benefit from Async data calls with Entity Framework.

Because of this, we decided to wrap the PoorDAO's in Repositories implementing an interface that exposes asynchronous methods. The idea is to keep the same interfaces for future EF asynchronous repositories :

// future common interface
public interface ICountryRepository
{
  Task<Country> GetAllCountries();
}

// current implementation hiding a PoorDAO in shame
public class CountryRepository : ICountryRepository
{
  public Task<Country> GetAllCountries()
  {
    var countries = PoorCountryDAO.GetAllcountries(); // poor static API call

    // some data transformation ...

    return Task.FromResult(result);
  }
}

What we have here is basically a synchronous operation hiding in asynchronous clothing. This is all fine, but my question is : while we're at it, wouldn't it be better to make the method entirely async and call await Task.Run(() => poorCountryDAO.GetAllcountries()) instead of just poorCountryDAO.GetAllcountries() ?

As far as I can tell, this would free up the IIS thread the Web Api service HTTP request is currently running on, and create or reuse another thread. This thread would be blocked waiting for the DB to respond instead of the IIS thread being blocked. Is that any better resource wise ? Did I totally misunderstand or overinterpret how Task.Run() works ?

Edit : I came across this article which claims that in some cases, asynchronous database calls can result in an 8 fold performance improvement. His scenario is very close to mine. I can't get my head around how that could be possible given the answers here and am a bit perplexed about what to do...

解决方案

Is that any better resource wise?

No; it's provably worse. The existing Task.FromResult and await is the best solution.

Task.Run, Task.Factory.StartNew, and Task.Start should not be used in an ASP.NET application. They steal threads from the same thread pool that ASP.NET uses, causing extra thread switches. Also, if they are long-running, they will mess with the default ASP.NET thread pool heuristics, possibly causing it to create and destroy threads unnecessarily.

这篇关于如果Web应用程序中的数据访问在不同的任务中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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