0

I am doing an MVC 5 APP and i am calling a Api Controller using PostAsJsonAsync like this

HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl + "Method/Action",param);

I got this error

[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]

I am doing this call from Global.asax Session_Start.

The intention is to register every person Access the site. I have to modify Session_Start to async void Session_Start

How can manage this situation? Making the call process Non-Asynchronous?

Diego
  • 2,238
  • 4
  • 31
  • 68

1 Answers1

0

I look here

and replace

public static async Task<long> Insert_SessionStart(
{
HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl + "Method/Action", param);

 if (response.IsSuccessStatusCode)
  return response.Content.ReadAsAsync<long>().Result;
  
  }

for this

public static long Insert_SessionStart()
{
 var response = client.PostAsJsonAsync(apiUrl + "Method/acti", param).Result;
  if (response.IsSuccessStatusCode)
   return response.Content.ReadAsAsync<long>().GetAwaiter().GetResult();
 
}

This way I avoid putting await Method in Session_start.

Diego
  • 2,238
  • 4
  • 31
  • 68