views:

104

answers:

1

Hi Everyone,

Referencing my Earlier Question, regarding downloading a file from a server and handling exceptions properly. I am positive that I had this solved, then in classic programming fashion, returned days later to frustratingly find it broken :-(


Updated code:

private static void GoGetIt(HttpContext context)
    {
        var directoryInfoOfWhereTheDirectoryFullOfFilesShouldBe = new FileInfo(......);
                var name = SomeBasicLogicToDecideName();

            //if (!directoryInfoOfWhereTheDirectoryFullOfFilesShouldBe.RefreshExists())
            //{
            //  throw new Exception(string.Format("Could not find {0}.", name));
            //}

            var tempDirectory = ""; //Omitted - creates temporary directory

            try
            {
                directoryInfoOfWhereTheDirectoryFullOfFilesShouldBe.CopyAll(tempDirectory);
                var response = context.Response;
                response.ContentType = "binary/octet-stream";
                response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.zip", name));
                ZipHelper.ZipDirectoryToStream(tempDirectory, response.OutputStream);
                response.End();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                context.Response.StatusCode = 404;
            }
            finally
            {
                tempDirectory.DeleteWithPrejudice();
            }
        }

This was working fine, and returning the zip, otherwise if the file didn't exist returning 404. Then on the client side I could handle this:

public bool Download()
{   
 try
                {
                    using (var client = new WebClient())
                    {
                        client.DownloadFile(name, tempFilePath);
                    }

                }
                catch (Exception)
                {
                    fileExists = false;
                }
return fileExists;
    }

But the problem now is two things.

1) I get System.Threading.ThreadAbortException: Thread was being aborted in the server side try-catch block. Usually this was just a file not found exception. I have no idea what or why that new exception is throwing?

2) Now that a different exception is throwing on the server side instead of the file not found, it would seem I can't use this set up for the application, because back on client side, any exception is assumed to be filenotfound.

Any help, especially info on why this ThreadAbortException is throwing!?!? greatly appreciated. Cheers

+1  A: 

The problem is that Response.End() throws a ThreadAbortException: that's how it ends the request. Just get rid of that call altogether, you don't need it.

Dean Harding
Well that was easy! Thanks. Though I swear I'm almost positive I had it working with that line there. Strange.
baron
Very strange. I have now changed the Server side catch to `catch (DirectoryNotFoundException dnfEx)` and left `response.End()` now the exception doesn't seem to throw any more?
baron
It still throws, but you're not catching it anymore. The runtime knows that a ThreadAbortException just means to end to the request and so doesn't report it.
Dean Harding
Ah that must have been what I did before when I was sure it was working with that line in and forgot I changed it. But like you mentioned, I should just get rid of it altogether then?
baron
Yeah, it's not needed. You might use it in some nested call somewhere as a quick way to cancel the request or something, but you don't need it in normal program flow.
Dean Harding