简体   繁体   中英

ASP .Net MVC Download Error and Require to Recycle IIS Application Pool

I currently maintain a nearly 3 years old ASP .Net MVC website, the application is running above IIS (now in IIS 7) and using ASP .Net 4 Framework . It used by client almost everyday and had a lot of upload-download file transaction. It also use ELMAH as Unhandled Exception Handling. The application running well until a few past month, there are a lot of report from user that they cannot do download the file, but without any error message, the download process just not doing anything while there is also no log in Browser Console. After doing several checking, all menu that have download function are using http response

        Response.Clear();
        Response.Cache.SetCacheability(HttpCacheability.Private);
        Response.Expires = -1;
        Response.Buffer = true;

        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Length", Convert.ToString(file_in_bytes.Length));

        Response.AddHeader("Content-Disposition"
                   , string.Format("{0};FileName=\"{1}\"", "attachment", fileName));
        Response.AddHeader("Set-Cookie", "fileDownload=true; path=/");

        Response.BinaryWrite(hasil);
        Response.End();

And nothing seems wrong (there are no Compile or Runtime Error in Development Server). We've also checked Elmah's log, but there no related error message appear in there. And This problem is temporarily disappear after our Server Management Team do Recycling the Application Pool in IIS .

This Web is also share Application Pool with another web, and when that error occurred, both application are affected, only the download function that affected , the other function like data retrieval from database, insert/edit/delete data is working fine.

I also checked the Web Server Event Viewer but there is nothing error in there. The very odd thing for us is that this error temporary disappear after we Recycling the Application Pool and after several days or weeks or months the error suddenly appear again.

Is there any log that we've missed to trace? or perhaps there is wrong with the Download code? And why its temporarily fixed after Recycling Application Pool?

Another Note : The data that need to be download by user is at average 500kb to 2MB in zip format contains several PDF files

Update : After few more hour investigating, I found that this web application using different method to Download, some are using the Http.Response like above code, and some are use FileContentResult as return value. But both using jquery.FileDownload in client-side. I also found this method in several Controller that has Download File method in this app,

private void CheckAndHandleFileResult(ActionExecutedContext filterContext) 
{
    var httpContext = filterContext.HttpContext;
    var response = httpContext.Response;

    if (filterContext.Result is FileContentResult)
    {
        //jquery.fileDownload uses this cookie to determine that 
        //a file download has completed successfully
        response.AppendCookie(new HttpCookie(CookieName, "true") 
            { Path = CookiePath });
    }
    else
    {
        //ensure that the cookie is removed in case someone did 
        //a file download without using jquery.fileDownload
        if (httpContext.Request.Cookies[CookieName] != null)
        {
            response.AppendCookie(new HttpCookie(CookieName, "true") 
                { Expires = DateTime.Now.AddYears(-1), Path = CookiePath });
        }
    }
}

Actually I'm not really sure is that method related to this error or not, but it is called in a method that override System.Web.MVC.Controller OnActionExecuted , and it contain the line off adding Cookie for file download if using FileContentResult or delete Cookie if is not using FileContentResult and file Download Cookie is exists. It is Possible if Cookie is Accidentally not deleted / cleared after it created? And because the download method is frequently called by nearly 100 user everyday, it is possible that the Cookie is pile up and cause IIS Worker Process Crash?

I've also checked some references about Cookie and its relation to IIS Session State (My Apps using In-Proc State). Am I Close? Or did I miss something?

Is there a reason why Response.Buffer is set to true? When buffering is enabled, the response is sent only after all processing is completed. Can you disable it by setting to false and see if this works? This could be the reason for having to recycle the app pool. You can also check if you are facing these issues - Help link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM