Search code examples
c#asp.net-mvcfileresult

Weird file download behavior in ASP.NET MVC application


I have an action method in my MVC app which returns a FileResult based on a file saved as a blob in the database. The root problem is that for some file types, requesting the file returns an empty file, even though I've verified that the database has the correct contents encoded in the Content column.

However, the really odd part is how difficult it is to me to pin down exactly what's causing this. Here's a few gotchas that I can't make sense of:

  • Downloading text files, like *.txt and *.csv returns an empty file, while downloading e.g. a PDF works correctly

  • This only happens on our CI server, and I can't reproduce it on my local workstation. These are the only differences I can think of:

    • I've verified that a commit that works on my machine, doesn't work on the CI server
    • I've tried connecting the local workstation to the CI server's database engine, and was not able to reproduce. In other words, the problem is in the code, rather than in the database (but as pointed out, the same code that works locally doesn't work on the CI server...).
    • The CI server runs IIS7, while I test locally on IIS Express that ships with Visual Studio

How do I trouble-shoot this? What could cause it?


Solution

  • OK, as expected from how the question is written, this problem was highly specific to our setup, so the actual fix for me is probably not that interesting. However, in order to (hopefully) be helpful to someone else stumbling over similar problems and finding their way here (welcome!), I'll post a summary of the troubleshooting campaign that eventually helped me find the root cause and fix it.

    tl;dr: The problem in this case was custom HTTP compression - when turned off, everything worked.

    Troubleshooting this type of problems - hints that would have helped me to resolve this faster:

    1. Are you sure that your local settings reflect those of the production/staging environment? Not just that they're supposed to, but that they actually do - if you're having problems reproducing the error, then it's almost certainly due to something here.

      Some things that could vary:

      • Build configurations: Debug/Release/OtherConf setting in build scripts? In IDE?
      • Web.config: we have a local Web.config file that is used by devs, but create the production version in our build script based on a template Web.config.erb. Do all versions match?
      • IIS settings: Do you have some custom settings in IIS on the production/staging server that handle serving of non-text/html data (or whatever it is that is failing)? Do you use the same settings locally?
         

      Still haven't found anything? Well, there are other ways to look for differences than manually.

    2. Try to side-step the CI build process, and publish your local (working) version of the app to the server. Hopefully, it can reveal some discrepancy that you missed.

    3. If you still cannot find a setting that differs, and still cannot reproduce it locally, can you debug on the server? If you don't have VS installed on the server, maybe give Remote debugging a try. (I didn't manage to get it working before I found the problem, mainly because it was excruciatingly slow on our setup, but it's well worth a try...)