Ani*_*mar 8 c# asp.net asynchronous async-await asp.net-mvc-5
我正在尝试使用async,await和task来实现我的Web应用程序中的异步需求之一.我的控制器类中有以下代码.我想要的是,在流程到达打印"控制器1"之后,系统在单独的线程上启动业务方法而不等待响应系统应该到达控制器中的打印语句并打印"控制器2".但是当我执行下面的代码时,我得到输出模式,因为 -
controller 1
PlandesignBS
controller 2
Run Code Online (Sandbox Code Playgroud)
虽然我期待这个 -
controller 1
controller 2 (Flow should reach here immediately rather than waiting
for business methods to get executed First)
Run Code Online (Sandbox Code Playgroud)
控制器类代码 -
public class PlanDetailsController : Controller
{
[HttpPost]
public async Task<ActionResult> Generate(PlanDetailsVM planDetailsVM)
{
System.Diagnostics.Debug.WriteLine("controller 1");
//calls business method to generate
quoteId = await Task.Run(() => _planDesignBS.GenerateBS(planDetailsVM));
System.Diagnostics.Debug.WriteLine("controller 2");
return Json(quoteId , JsonRequestBehavior.AllowGet);
}
}
Run Code Online (Sandbox Code Playgroud)
商业方法代码 -
public int GenerateBS(PandetailsDTO plandetails)
{
System.Diagnostics.Debug.WriteLine("PlandesignBS");
//STEP 1-> call to dataLogic Layer to use Entity Framework
// STEP 2-> call to a method (this method is part of dll used
//in our project and we don't have any control on it)
return quoteId
}
Run Code Online (Sandbox Code Playgroud)
编辑:我试图这样做的原因是我想从我的客户端使用几个http帖子请求.假设我首先从客户端调用Generate action方法,然后不想等待它的响应,同时想要调用另一个http post.
编辑:包括EF代码,当我试图遵循一些解决方案时,我正在获取execption.这个方法将从我的GenerateBS方法调用.我在getSICCode方法中的这一行得到例外 -
DbContext.Database.ExecuteSqlCommand("spGET_SICCode @industryClasifID,@industrySubClasifID,@SICCode OUTPUT", industryClasifID, industrySubClasifID, SICCode);
Run Code Online (Sandbox Code Playgroud)
EF代码 -
public class PlanDesignRepository : Repository<myobject>, IPlanDesignRepository
{
private IDbSet<myobject> _dbSet;
private DbContext _dbContext;
private IDbSet<myobject> DbSet
{
get
{
if (_dbSet == null)
{
_dbSet = base.UnitOfWork.Context.Set<myobject>();
}
return _dbSet;
}
}
private DbContext DbContext
{
get
{
if (_dbContext == null)
{
_dbContext = base.UnitOfWork.Context;
}
return _dbContext;
}
}
public string GetSICCode(IndustryClassficDO industryClassficDO)
{
SqlParameter industryClasifID = new SqlParameter("industryClasifID", SqlDbType.Int);
industryClasifID.Value = industryClassficDO.IndustryClassificationId;
SqlParameter industrySubClasifID = new SqlParameter("industrySubClasifID", SqlDbType.Int);
industrySubClasifID.Value = industryClassficDO.IndustrySubClassificationId;
SqlParameter SICCode = new SqlParameter("SICCode", SqlDbType.VarChar, 10);
SICCode.Direction = ParameterDirection.Output;
DbContext.Database.ExecuteSqlCommand("spGET_SICCode @industryClasifID,@industrySubClasifID,@SICCode OUTPUT", industryClasifID, industrySubClasifID, SICCode);
return (string)(SICCode.Value);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我有几个http Post电话,如下所示 -
AngularJS code for AJAX calls:
$http({
url: key_Url_Generate,
method: Post,
params: $scope.PlanDetails
}).then(function (result) {
//Notify user for success or failure
}
Run Code Online (Sandbox Code Playgroud)
await的使用意味着您在继续代码之前等待任务完成.如果您希望它与其他代码同时执行,您应该像这样更改它
public class PlanDetailsController : Controller
{
[HttpPost]
public async Task<ActionResult> Generate(PlanDetailsVM planDetailsVM)
{
System.Diagnostics.Debug.WriteLine("controller 1");
//calls business method to generate
var task = Task.Run(() => _planDesignBS.GenerateBS(planDetailsVM));
System.Diagnostics.Debug.WriteLine("controller 2");
var quoteId = await task;
return Json(quoteId , JsonRequestBehavior.AllowGet);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我不建议这样做,因为它没有任何意义,并且可能通过使ASP.NET线程池线程匮乏而降低性能.你为什么要这样做呢?