Problem with async task

Asked

Viewed 181 times

1

I’m banging my head to solve but I can’t, the mistake is

An asynchronous module or handler completed while an asynchronous operation was still pending.

source error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I am sending a request to generate a billet, but it is asynchronous error. I am using the plugin Fluent for that reason

code:

public async Task<FileStreamResult> Teste(){
    var response = url.WithHeaders(
        new
        {
            Authorization =
            "Basic " + Convert.ToBase64String(
                Encoding.UTF8.GetBytes(ApiKey + ":" + Password)),
            ContentType = "application/x-www-form-urlencoded"
        }).PostUrlEncodedAsync(boleto).Result;

    var pdfBytes = response.Content.ReadAsByteArrayAsync().Result;
    pdfBytesResult = pdfBytes;
    MemoryStream ms = new MemoryStream(pdfBytesResult);
    return new FileStreamResult(ms, "application/pdf");
}

The problem only occurs when the site is in production(IIS), when local the same does not happen

  • Try never to use .Result of a Task when you want to work asynchronously, in such cases you are forcing the code to be synchronous, and forcing something synchronous into something asynchronous worsens the performance, it is worse than working directly with synchronism.

2 answers

2

You need to use the await to await the execution of ReadAsByteArrayAsync

var pdf = await response.Content.ReadAsByteArrayAsync();
  • Of error: 'byte[]' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'byte[]' could be found (are you missing a using directive or an assembly reference?)

  • 2

    The right thing is: await response.Content.ReadAsByteArrayAsync()

-1


The problem was not in the code but in the proxy, which was blocking external access.

  • Dude, this has nothing to do with the mistake you posted at the beginning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.