2

I have a controller method that is doing a db lookup for user that is logged in, then it will send a confirmation email, looks like this:

[HttpPost]
public async void ResendConfirmationEmail(string username)
{
    var user = await Usermanager.FindByNameAsync(username);
    try { SendRegistrationEmail(user); }
    catch (Exception e)
    {
        //TODO: LOGGIN EMAIL ERROR
        throw e;
    }
}

I want to call this from a ajax function like this:

var ResendRegEmail = function (identity) {
    $.ajax({
        type: "POST",
        url: "/Customer/ResendConfirmationEmail",
        data: { username: identity },
        success: function (data) {
            $("#ResendModal").modal('show');
            $("#msgSuccess").html("Email has been sent succesfully please check your email.");
        },
        error: function (request, textStatus, errorThrown) {
            $("#ResendModal").modal('show');
            $("#msgError").html(textStatus + "   " + errorThrown);
        }
    })
}

[AllowAnonymous]
public async void SendRegistrationEmail(JustAskUser user)
{ code here }

if I remove the async the "resendCOnfirmationEmail" function works as expected, but the business requirement is using async to keep the flow of the site as fast as possible. Currently receiving a resource wont load 500 error

but then i get an error from "sendregistrationEmail" - {"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" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it."}

3
  • 3
    You don’t need to write async action methods when calling them through AJAX, as AJAX is asynchronous by default Commented Mar 13, 2019 at 20:45
  • well damn.....i was trying to be all sophisticated by using async in my DB calls... but this seems like i just did overkill and made myself problems. Thank you... should i delete this? Commented Mar 13, 2019 at 20:55
  • @DavideVitali an async MVC controller action has nothing to do with the asynchronicity of AJAX. It is the async principle to the C# code itself. Commented Mar 13, 2019 at 22:38

1 Answer 1

2

I believe your issue to be the line where you call SendRegistrationEmail. Because this method is async you should have an await keyword before the method name.

The error message contains the solution as stated.

... Instead, the asynchronous method should return a Task, and the caller should await it

So your method should be modified like so:

[HttpPost]
public async Task ResendConfirmationEmail(string username)
{
    var user = await Usermanager.FindByNameAsync(username);
    try 
    { 
        await SendRegistrationEmail(user); 
    }
    catch (Exception e)
    {
        //TODO: LOGGIN EMAIL ERROR
        throw e;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes that is part of it as the comments state above i didnt have to make it async to begin with because ajax itself is async. I removed async from them both and it works beautifully. Thank you for your response.
Your confusing Ajax's A with the async keyword. They have nothing to do with each other. The async keyword makes your C# and only your C# code run in an asynchronous manner. I suggesting reviewing this document to understand what I am saying. learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.