question

InigoMontoya-1790 avatar image
0 Votes"
InigoMontoya-1790 asked AgaveJoe answered

Make sendPostFailMessage Async?

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");
     }
 }
dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your code does not follow the async/await programming pattern. I recommend reading Asynchronous programming with async and await.

 public class SendEmail
 {
     public async Task sendPostFailMessageAsync()
     {
         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 = await client.SendEmailAsync(msg);
     }
 }


0 Votes 0 ·

@AgaveJoe - 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?

0 Votes 0 ·

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.