dot net mvc5 控制器中的异步函数创建死锁
Posted
技术标签:
【中文标题】dot net mvc5 控制器中的异步函数创建死锁【英文标题】:Async function in dot net mvc5 controller creating deadlock 【发布时间】:2019-04-07 07:21:31 【问题描述】:我正在将此库用于孟加拉国支付网关。https://github.com/Rahim373/Arts.SslCommerze .但是控制器中的代码等待iis无限时间。如果响应正确地从服务器到达我的机器,我检查了提琴手。它正在到达。但是异步函数没有执行。 我的代码是:
public ActionResult About()
string customerName = "Fahim Abrar";
string customerEmail = "fahimabrar13@gmail.com";
string customerPhone = "+8801853912845";
string transactionId = "45c2ffc4d";
string successUrl = "http://fahimabrar.com";
string failUrl = "http://fahimabrar.com";
string cancelUrl = "cancelUrl";
decimal amount = 50;
Customer customer = new Customer(customerName, customerEmail,
customerPhone);
EmiTransaction emiTransaction = new
EmiTransaction(isEmiEnabled: false);
Trasnaction trasnaction = new Trasnaction(amount,
transactionId, successUrl, failUrl,
cancelUrl, emiTransaction, customer);
SslRequest.GetSessionAsync(trasnaction).ConfigureAwait(continueOnCapturedContext: false);
var session = SslRequest.GetSessionAsync(trasnaction).Result;
string s = session.FailedReason;
ViewBag.Message = s;
//"Your application description page.";
return View();
这里 var session = SslRequest.GetSessionAsync(trasnaction).Result; 此行导致死锁。
【问题讨论】:
【参考方案1】:.Result
会导致死锁。
将您的方法设为async
并使用await
例如
public async Task<ActionResult> About()
string customerName = "Fahim Abrar";
string customerEmail = "fahimabrar13@gmail.com";
string customerPhone = "+8801853912845";
string transactionId = "45c2ffc4d";
string successUrl = "http://fahimabrar.com";
string failUrl = "http://fahimabrar.com";
string cancelUrl = "cancelUrl";
decimal amount = 50;
Customer customer = new Customer(customerName, customerEmail,
customerPhone);
EmiTransaction emiTransaction = new
EmiTransaction(isEmiEnabled: false);
Trasnaction trasnaction = new Trasnaction(amount,
transactionId, successUrl, failUrl,
cancelUrl, emiTransaction, customer);
var session = await SslRequest.GetSessionAsync(trasnaction);
string s = session.FailedReason;
ViewBag.Message = s;
//"Your application description page.";
return View();
见:Using Asynchronous Methods in ASP.NET MVC
【讨论】:
给出错误“ 由于未等待此调用,因此在调用完成之前继续执行当前方法。考虑将'await'运算符应用于调用结果。 在哪里,@FahimAbrar? 在视觉工作室构建中 你想await
那一行(并创建方法async Task<...>
);不要使用.Result
以上是关于dot net mvc5 控制器中的异步函数创建死锁的主要内容,如果未能解决你的问题,请参考以下文章
我在 asp dot net core 中创建控制器但出现错误