在MVC 6 ASP.NET 5 beta 4中使用TryUpdateModelAsync()的死锁

时间:2021-08-14 14:47:58

After upgrading from Beta3 to Beta4, I'm now getting what looks like a deadlock(site hanges indefinitely) when calling TryUpdateModelAsync

从Beta3升级到Beta4后,我现在正在调用TryUpdateModelAsync时看起来像死锁(网站无限期地变化)

public ActionResult Edit(ContactModel contactModel)
    {
        var contact = db.Contact.Find(contactModel.ContactId);

        if (ModelState.IsValid)
        {
            TryUpdateModelAsync(contact);
            var result = db.SaveChanges(() => { });

            if (result.Success)
                return RedirectToAction("Details", "Client", new { id = contact.ClientId });
        }
        return View(contactModel);
    }

original code above was ported from MVC 5 so I was just trying to run synchronously. I get the same result after converting the controller method to Async like this

上面的原始代码是从MVC 5移植的,所以我只是试图同步运行。将控制器方法转换为Async之后,我得到了相同的结果

public async System.Threading.Tasks.Task<ActionResult> Edit(ContactModel contactModel)
    {
        var contact = db.Contact.Find(contactModel.ContactId);

        if (ModelState.IsValid)
        {
            var updateResult = await TryUpdateModelAsync(contact);
            var result = db.SaveChanges(() => { });

            if (result.Success)
                return RedirectToAction("Details", "Client", new { id = contact.ClientId });
        }
        return View(contactModel);
    }

I took a quick look through the sample projects and I couldn't find any usage of this method, anyone have a working example? or know why this is happening?

我快速浏览了示例项目,但是我找不到这种方法的用法,有没有人有一个有效的例子?或者知道为什么会这样?

1 个解决方案

#1


Here is a sample of usage of try update model async (in MVC test code), Note this is from the dev branch. You might have to switch to whatever branch you are on.

以下是try update model async(在MVC测试代码中)的使用示例,请注意这是来自dev分支。您可能必须切换到您所在的任何分支。

https://github.com/aspnet/Mvc/blob/dev/test/WebSites/ModelBindingWebSite/Controllers/TryUpdateModelController.cs

Your first sample is bad because updating the model async without an await will create a task, but continue over it to the next method.

您的第一个示例是错误的,因为在没有await的情况下更新模型async会创建一个任务,但会继续执行下一个方法。

To figure out why you have a deadlock you want to break into the deadlock and see where is the stack at, does it have to do with tryupdatemodel at all?

要弄清楚为什么你有一个死锁,你想要进入死锁,看看堆栈在哪里,它是否与tryupdatemodel有关?

If you can create a simple repro in a small project please file a bug in https://github.com/aspnet/mvc/issues

如果您可以在一个小项目中创建一个简单的repro,请在https://github.com/aspnet/mvc/issues中提交一个错误

#1


Here is a sample of usage of try update model async (in MVC test code), Note this is from the dev branch. You might have to switch to whatever branch you are on.

以下是try update model async(在MVC测试代码中)的使用示例,请注意这是来自dev分支。您可能必须切换到您所在的任何分支。

https://github.com/aspnet/Mvc/blob/dev/test/WebSites/ModelBindingWebSite/Controllers/TryUpdateModelController.cs

Your first sample is bad because updating the model async without an await will create a task, but continue over it to the next method.

您的第一个示例是错误的,因为在没有await的情况下更新模型async会创建一个任务,但会继续执行下一个方法。

To figure out why you have a deadlock you want to break into the deadlock and see where is the stack at, does it have to do with tryupdatemodel at all?

要弄清楚为什么你有一个死锁,你想要进入死锁,看看堆栈在哪里,它是否与tryupdatemodel有关?

If you can create a simple repro in a small project please file a bug in https://github.com/aspnet/mvc/issues

如果您可以在一个小项目中创建一个简单的repro,请在https://github.com/aspnet/mvc/issues中提交一个错误