使用异步存储库 class 引发错误

Using Async Repository class is raising An Error

我的 asp.net mvc web 应用程序中有以下操作方法:-

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Deactivate(Staff staff)
{
  try
  {
    if (staff == null)
    {
      return Json(new { IsSuccess = "custome", id = 1, description = "Error occurred: Record has already been deleted." }, JsonRequestBehavior.AllowGet);
    }

    staffRepository.DeactivateStaff(staff);
    staffRepository.Save();

    return RedirectToAction("Index");
  }
}

和以下存储库 classes:-

public void DeactivateStaff(Staff staff)
{
  staff.ISActive = false;
  context.Entry(staff).State = EntityState.Modified;
}

public async void Save()
{
  await context.SaveChangesAsync();
}

和以下摘要class:-

public interface IStaffRepository : IDisposable
{
  void Save();
  IQueryable<Staff> GetStaffForGrid(string filter, int page, int pageSize, string sort, string sortdir,bool isdeleted,bool isactive);
  int GetStaffForGridCount(string filter, bool isdeleted, bool isactive);
  SyncWithAD SyncUsersWithAD(string term = null);
  void DeactivateStaff(Staff staff);
  Task<Staff> FindStaff(int id,Byte[] timestamp);
}

现在控制器中的以下方法 staffRepository.Save(); 引发了以下异常:-

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

谁能给点建议?

编辑 我的存储库操作方法是:-

public async Task DeactivateStaff(Staff staff)
              {

                  staff.ISActive = false;
                  context.Entry(staff).State = EntityState.Modified;

              }

            public async Task Save()
            {
                await context.SaveChangesAsync();
            }

我的控制器动作方法是:-

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Deactivate(Staff staff)
        {
try
            {
                if (staff == null)
                { return Json(new { IsSuccess = "custome", id = 1, description = "Error occurred: Record has already been deleted." }, JsonRequestBehavior.AllowGet); }

                staffRepository.DeactivateStaff(staff);
               staffRepository.Save();

                return RedirectToAction("Index");
            }

目前我将在 VS 中的 Deactivate 方法中收到以下警告:-

Warning 1 This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread

当我 运行 我的应用程序和我调用操作方法时,不会引发任何异常,不会影响数据库?我的意思是不会执行更新操作,请问您可以吗? 谢谢

我有一个 MSDN article that describes the common causes of that error - one of which is async void methods. I have another MSDN article 详细解释了为什么开发人员应该避免 async void

因此,您只需使用 Task 而不是 void。让您的任务返回方法以 Async:

结尾也是一个好主意
public interface IStaffRepository : IDisposable
{
  Task SaveAsync();
  ...
  Task<Staff> FindStaffAsync(int id, byte[] timestamp);
}

实现可以保持不变:

public async Task SaveAsync()
{
  await context.SaveChangesAsync();
}

或者你可以稍微简化一下:

public Task SaveAsync()
{
  return context.SaveChangesAsync();
}

并且您的控制器方法变为 async:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Deactivate(Staff staff)
{
  try
  {
    ...

    staffRepository.DeactivateStaff(staff);
    await staffRepository.SaveAsync();

    return RedirectToAction("Index");
  }
  ...
}