简体   繁体   English

不使用异步功能时出现异步错误?

[英]Getting an Async error when I'm not using an Async function?

I have this function as part of a api to update data after a payment has been confirmed. 确认付款后,我将此功能作为api的一部分来更新数据。 It all works and updates the data but fails to redirect to the specified URl and throws this error: 所有这些都可以工作并更新数据,但是无法重定向到指定的UR1并引发以下错误: 在此处输入图片说明

I don't understand why I'm getting it as I'm not using async anywhere? 我不明白为什么要得到它,因为我在任何地方都不使用异步? Help would be appreciated. 帮助将不胜感激。

public HttpResponseMessage PaymentConfirmed(string id, string email, string status, string amount, string product)
{
    if(status == "Paid")
    {
        var Id = id;
        var Email = email;

        var uid = Convert.ToInt32(Id);
        var userbyId = RepositoryHelper.GetPersonFromId(uid);
        if (userbyId.UserType == "Donation")
        {
            RepositoryHelper.UpdateDonation(userbyId, amount);
        }
        if (userbyId.UserType == "Sponsor")
        {
            var school = RepositoryHelper.GetSchoolByUser(userbyId);
            RepositoryHelper.AddFreeUsers(school);
        }
        if (userbyId.UserType == "School" || userbyId.UserType == "Pupil")
        {
            var complete = RepositoryHelper.UpgradeToPaid(userbyId);
        }

        SendUserPaymentConfirmed(userbyId);

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Moved);
        response.Headers.Location = new Uri("http://url here");
        return response;
    }
    return null;
}

You can try by putting an await in front of these method calls like: 您可以尝试在这些方法调用之前放置一个等待,例如:

await RepositoryHelper.UpdateDonation 
await RepositoryHelper.AddFreeUsers
await RepositoryHelper.UpgradeToPaid
await SendUserPaymentConfirmed

Then in method declaration change it : 然后在方法声明中更改它:

public Task<HttpResponseMessage> PaymentConfirmed

Build the project and fix where there are errors. 构建项目并修复存在错误的地方。 Most likely the methods that does not support async will complain or will have errors. 不支持异步的方法很可能会抱怨或出现错误。 Then run the API again and see if you still get the same error. 然后再次运行API,看看是否仍然出现相同的错误。

user error!!!!!! 用户错误!!!!

Found the issue, although my send message returned a void it had this nestled within it client.SendAsync(mailMessage, null); 发现了问题,尽管我的发送消息返回了一个无效值,但它client.SendAsync(mailMessage, null);在其中client.SendAsync(mailMessage, null);

Removed it and all works fine 删除它,一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在异步函数中使用async? - Using async in async function? 使用异步方法返回值时如何返回值。 我收到一个错误 - How do I return the value when using async method to return the value. I am getting an error 将异步BCL与异步功能一起使用 - Using async BCL with async function 异步功能未被调用 - Async Function Not Getting Called 使用ReadAsStringAsync()时如何避免将方法设置为异步? - How can avoid set my method as async when I'm using ReadAsStringAsync()? 在async函数上使用await时出现异常 - Exception when using await on async function 尝试在使用.net mixpanel项目时使用异步/等待调用方法时收到错误消息 - Getting error message when trying to call a method using async/await when using a .net mixpanel project 如果我从Content-Disposition获取原始文件名,则在异步下载第二个文件时WebClient冻结 - WebClient freezes when async-downloading a second file if I'm getting the original filename from Content-Disposition 为什么我在使用 async/Task 时在 Azure Function 中出错,但在 Console App 中使用它们时却没有错误? - Why I get error in Azure Function when use async/Task, but when use these in Console App, there is no error? 使用Async时超时 - Timeout when using Async
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM