Error 502 on Azure with context.Response.BinaryWrite(buffer); RRS feed

  • Question

  • Hi all,

    I hope that you could help me. 

    I've a function in my web app to store the file directly into the SQL database (it could be images or pdf). In some other pages, the files could be open by clicking on a link but on Azure the following error happens (on the line -> context.Response.BinaryWrite(buffer)). On local it's work fine. 

    502 - Web server received an invalid response while acting as a gateway or proxy server.

    There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

    Here is the code I used for opening the file:

                   //tf is the entity load from the SQL database

                   Byte[] buffer = tf.Data;
                    context.Response.ContentType = tf.ContentType;
                    context.Response.Charset = "UTF-8";
                    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    context.Response.AddHeader("content-disposition", "attachment; filename=" + tf.FileName);
                    context.Response.AddHeader("content -length", buffer.Length.ToString());
                    context.Response.BinaryWrite(buffer); // error happens here

    Is there Something to configure on my azure app service? Or do I need to open the file by another way (but how?)?

    Thank you a lot for your help!


    Thursday, August 3, 2017 3:02 PM


All replies

  • As you are using Azure web app, you can use kudu console to diagnose the issue: https://github.com/projectkudu/kudu/wiki/Diagnostic-Log-Stream.

    Also, you can enable diagnostic logs to get the logs: https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-enable-diagnostic-log.

    Check to see if you have any unused namespaces in your code (grayed out namespaces), remove those, republish and see if it helps.

    Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members.

    • Proposed as answer by Swikruti Bose Thursday, August 3, 2017 4:13 PM
    Thursday, August 3, 2017 4:13 PM
  • Thank you for your answer.

    Unfortunatly even by turning the log-Stream, I don't see any detail about the 502 error I got. I will try again tomorrow.

    I removed unused namespace for the corresponding page (the one that crashed). Do I need to check for every page? could that be the problem?

    By the way my site works well everywhere else, it's just when I called the BinaryWrite that it crashed.

    By debbuging the process I know for sure that this is the line context.Response.BinaryWrite(buffer); that failed. Perhaps there is Something to configure into the Azure Portal to allow the binaryWrite?

    • Edited by annemu Monday, August 7, 2017 11:54 AM
    Monday, August 7, 2017 11:51 AM
  • I resolved my problem by changing my code and opening my files by using an ashx file like in the example (option ImagesTable) : https://code.msdn.microsoft.com/How-to-store-the-in-SQL-6c6a46b5

    Thanks for your help.

    • Marked as answer by annemu Tuesday, August 8, 2017 9:43 AM
    Tuesday, August 8, 2017 9:43 AM
  • Glad to know that your issue has been resolved. Appreciate for sharing the solution which might help other customers with the similar issue.


    Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members.

    Tuesday, August 8, 2017 11:51 AM