-
Notifications
You must be signed in to change notification settings - Fork 582
Description
This is similar to issue #235
Originally, under 7.0.1 I found that when running as a synchronous operation the system would stop processing when it would hit the POST method on SendGridClient. I found that it would process if I ran the task in its own thread, but when running in its own thread I would get the following exception:
System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.
I updated today to version 7.0.3 using the synchronous operation, it continued processing the page as expected, however; was presented with the same error message as though it was processed from a second thread.
Error Message:
System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.
Below is some code (I am posting more than what is probably necessary, but it will give you more to look at):
ApiBaseController.cs:
protected BLL.EmailClient.IEmailClient EmailClient
{
get
{
return this.emailClient;
}
}
public ApiBaseController()
{
emailClient = BLL.EmailClient.Email.GetEmailClient(new BLL.EmailClient.EmailOptions()
{
APIKey = ConfigurationManager.AppSettings["SgBacchusAPIKey"]
});
}
ProvisionController.cs:
public IHttpActionResult Post(DTO.Registration.ProvisionOptions options)
{
this.provision = options;
provisionHub = GlobalHost.ConnectionManager.GetHubContext<ProvisionHub>();
BeginProvisioningProcess(new ApplicationDbContext());
return Ok();
}
async private void BeginProvisioningProcess(ApplicationDbContext context)
{
try
{
SendHubMessage("Starting...");
var company = CreateCompany(context);
var provisioningHandler = new ProvisioningHandler[]
{
CreatePropertySettings,
CreateUserAccountAsync
};
// Chain the logic handler together.
var chained = (ProvisioningHandler)Delegate.Combine(provisioningHandler);
// Loop through each item and see if it fails or not.
var successful = true;
foreach (ProvisioningHandler chainableLogicCall in chained.GetInvocationList())
if (!(await chainableLogicCall(company, context)))
{
successful = false;
break;
}
if (successful)
{
context.SaveChanges();
//await SendEmailAsync(emailFromAddress, provision.UserName, Resources.LanguagePack.AccountCreatedSuccessSubject, Resources.LanguagePack.AccountCreatedSuccessMsg);
EmailClient.SendMessage(emailFromAddress, provision.UserName, Resources.LanguagePack.AccountCreatedSuccessSubject, Resources.LanguagePack.AccountCreatedSuccessMsg);
}
else
{
var msg = new StringBuilder();
msg.AppendLine("Name: " + provision.CompanyName);
msg.AppendLine("Contact Phone Number: " + provision.PhoneNumber);
msg.AppendLine("Contact email: " + provision.UserName);
msg.AppendLine("Output Message: " + outputMessage);
//await SendEmailAsync(emailFromAddress, supportEmailAddress, "Account Setup Failure", msg.ToString());
}
}
catch (Exception ex)
{
var msg = new StringBuilder();
msg.AppendLine("Name: " + provision.CompanyName);
msg.AppendLine("Contact Phone Number: " + provision.PhoneNumber);
msg.AppendLine("Contact email: " + provision.UserName);
msg.AppendLine("Error Message: " + ex.Message);
//await SendEmailAsync(emailFromAddress, supportEmailAddress, "Account Setup Failure", msg.ToString());
}
finally
{
SendHubMessage("Complete");
}
}
SendGridClient.cs
public override void SendMessage(string emailFrom, string emailRecipient, string subjectLine, string HTMLMessage)
{
dynamic sg = new SendGridAPIClient(options.APIKey, "https://api.sendgrid.com");
SendGrid.Helpers.Mail.Email from = new SendGrid.Helpers.Mail.Email(emailFrom);
SendGrid.Helpers.Mail.Email to = new SendGrid.Helpers.Mail.Email(emailRecipient);
string subject = subjectLine;
Content content = new Content("text/html", HTMLMessage);
Mail mail = new Mail(from, subject, to, content);
try
{
string ret = mail.Get();
dynamic response = sg.client.mail.send.post(requestBody: ret);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Now the email is sent successfully, however; the overall response for the call to POST results in a failure. I think this is likely due to a POST to SendGridClient while doing a POST to ASP.NET WebAPI.