Custom FileResult on Azure: Browser Waits forever

Sep 4, 2012 at 5:01 PM

First of all thanks for a great tool.

I got a problem with my deployment of an MVC web application on Azure.

I have an action that returns an Excel as a custom FileResult (XlsxResult). My solution is based on the ClosedXml. My XlsxResult class uses a read-only .xlsx file on the server as a template. It then passes on the template into a memory stream that gets manipulated and saved back with ClosedXml. In the end the memory stream get written to the response.

This works fine both on Cassini as well as IIS Express but fails when deployed on azure with no error whatsoever. The only effect I am experiencing is the request sent to the server never gets any response. I am still waiting for something to happen after 60 minutes or so...

you can read more details as well as the code used it in stackoverflow here.

Thanks in advance

Constantinos

Coordinator
Sep 4, 2012 at 5:09 PM

Download the source code and debug the save method to see where it's hanging. Check out http://stackoverflow.com/questions/2949902/debugging-deployed-azure-app

Sep 4, 2012 at 5:57 PM

Wow that was quick!! :-)

I am on it. Thanks for the advice

Will post an update when I find anything.

Thnaks

Sep 25, 2012 at 3:48 PM
Edited Sep 25, 2012 at 3:56 PM

Hi,

I did check the intellitrace logs from Azure but did not see an exception.

I also used the Process Monitor v3.03  to check the w3wp.exe and file access. 

Since I like a lot the closedxml library I feell I must report the following before changing to an alternative:

  1. The export process for a simple operation of writing a text value inside a cell "A1" gets stuck with no stackoverflow on windows azure. The process takes over full cpu on the server (whine inside the ClosedXML lib). I got a 2 core instance and one of the cores limits up. I can only guess that there is an infinite loop somewhere. Maybe the CopyStram method inside the XLWorkbook class.
    internal static void CopyStream(Stream input, Stream output)
            {
                var buffer = new byte[8 * 1024];
                int len;
                while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
                    output.Write(buffer, 0, len);
            }
    
  2. In debug mode from visual studio where the export works as expected. The "export from a template" mechanism should be as simple as the following code sample. But in this case the changes are ignored and it outputs only the original TemplateFile:

protected override void WriteFile(System.Web.HttpResponseBase response) {
            try {
                using (var fs = new FileStream(templatePath, FileMode.Open, FileAccess.Read)) {
                    using (var workbook = new XLWorkbook(fs)) {
                        var ws = workbook.Worksheets.Worksheet("Foward Curve");
                        ws.Cell("A1").Value = "test value";
                        workbook.SaveAs(response.OutputStream);
                    }
                }
            } catch (Exception ex) {
                System.Diagnostics.Trace.Write(ex);
            }
        }

 Instead it only works with a temporary file or memory stream as seen in the following codeblock:

protected override void WriteFile(System.Web.HttpResponseBase response) {
            try {
                string tempFile = Path.Combine(this.localResource.RootPath, string.Format("{0}.xlsx", Guid.NewGuid()));
                using (var fs = new FileStream(templatePath, FileMode.Open, FileAccess.Read)) {
                    using (var workbook = new XLWorkbook(fs)) {
                        var ws = workbook.Worksheets.Worksheet("Foward Curve");
                        ws.Cell("A1").Value = "test value";
                        workbook.SaveAs(tempFile);
                    }
                }
                using (var fs = new FileStream(tempFile, FileMode.Open, FileAccess.Read)) {
                    fs.CopyTo(response.OutputStream);
                }
                File.Delete(tempFile);
            } catch (Exception ex) {
                System.Diagnostics.Trace.Write(ex);
            }
        }

Thanks again for your time,

Constantinos

Coordinator
Sep 25, 2012 at 5:05 PM

I really wish I could help you but I have no idea why it's behaving that way in Azure. If you're able to pinpoint the problem I'd be happy to make the changes.

I'm sorry for I know it's not what you wanted to hear...