Make sendPostFailMessage Async?

Johnathan Simpson 586 Reputation points
2021-08-05T19:56:36.443+00:00

Hello - how can I make this method async?

public class SendEmail
{
    public void sendPostFailMessage()
    {       var apiKey = "";

        var client = new SendGridClient(apiKey);
        var from = new EmailAddress("test123@gmail.com", "Test");
        var subject = "Error";
        var to = new EmailAddress("Test221@gmail.com", "Testtest");
        var plainTextContent = "and easy to do anywhere, even with C#";
        var htmlContent =
            "Hello, <br><br> There has been an is";
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
        var response = client.SendEmailAsync(msg).Wait(10000);
    }
}

public class StartService : IStartService
{
    private readonly SendEmail _sendEmail;
    public async Task GenerateAndSendJson()
    {
        foreach (var item in fromDatabase)
        {
            Console.WriteLine("Sending data");
            try
            {

            }
            catch
            {
                _sendEmail.sendPostFailMessage();
                continue;
            }
        }
        Console.WriteLine("Process finish");
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,354 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 26,161 Reputation points
    2021-08-06T00:52:17.59+00:00

    and since I await the response in the sendPostFailMessageAsync() method, I do not need to await it in the calling method of GenerateAndSendJson()? Is that an accurate statement?

    Yes. The calls must be asynchronous from bottom to top. This information is openly covered in the linked documentation.

    0 comments No comments