0
Hello, I have some questions regarding the code below.
- The return code the following error [Invalidoperationexception]: Asynchronous module or manipulator completed while asynchronous operation was pending.
- The main method that calls this task(this other methods) returns an Ihttpactionresult and the answer of the Smsasync method ends up interfering in the answer of my main method, that is, I would like to return status 200 even with error in the SMS method - I’ve tried Try Cath without any action on Cath;
The first item, I solved using the call of this method with a Task.Factory.Startnew, since in this case, I do not need the answer of the method to follow but and if this were not the case ?
public async void SMSAsync(String mobileNumber)
{
Guid gui = Guid.NewGuid();
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization", "Basic XPTO");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
using (var content = new StringContent("{ " +
"\"sendSmsRequest\": { " +
"\"from\": \"WeBlank\", " +
"\"to\": \"55" + mobileNumber + "\", " +
"\"schedule\": \"2014-08-22T14:55:00\", " +
"\"msg\": \"Aqui vai a mensagem de SMS.\", " +
"\"callbackOption\": \"NONE\", " +
"\"id\": " +
"\"" + gui.ToString() + "\", " +
"\"aggregateId\": \"1111\", " +
"\"flashSms\": false }}", System.Text.Encoding.UTF8,
"application/json"))
{
using (var response = await httpClient.PostAsync("https://api-rest.zenvia360.com.br/services/send-sms", content))
{
string responseData = await response.Content.ReadAsStringAsync();
}
}
}
}
https://gist.github.com/carloshenriqueribeiro/0b879e1655e7eedd605e264d50a45742
How do I call the method
public IHttpActionResult CreateUser(ParticipanteCreateModel participante)
{
// código que executa a criação do usuário
String telefone;
telefone = part.Telefone.Replace('-', ' ');
telefone = telefone.Replace('(', ' ');
telefone = telefone.Replace(')', ' ');
telefone = telefone.Replace(" ", String.Empty);
Task.Factory.StartNew(() => SMSAsync(telefone));
return Ok(usuarioId);
}
Although you are abusing
using
, I couldn’t reproduce the behavior here... the method runs smoothly and at the end I get the access response denied (What was expected)– Leandro Angelo
Include your action code, how you are invoking the method and assigning your response
– Leandro Angelo
Possible duplicate of Asynchronous module or handler completed while asynchronous operation was pending
– Barbetta
@Leandroangelo code is an adaptation of the service provider’s website (zenvia) https://zenviasms.docs.apiary.io/#Introduction/status table/detailcode
– Carlos Henrique
@Barbetta is not duplicate question. I read this item before opening my question still, thank you.
– Carlos Henrique