System.Web.HttpException: The remote host closed the connection. The error code is 0x800704CD

0

I looked over all possible answers and I got nothing that could lead me to a solution about that volatile problem.

In the MVC5 web application I work, some users can download an Excel file that is generated when they press a download button. At the moment of writing, the web application do not store the file on the server. So the users need to wait until it has been completed to see it as a successful downloaded file.

I will paste the code below. The problem is that the download method create an

500 - Connection Timed out

if it appears to be the first time since a long time this method as not been hit. If a user press the browser back button and do it again, the download time will be very short and the problem will come back many hours later if nobody downloaded their Excel file.

When it does a connection timed out, I got error reports. The first one is :

System.Web.HttpException: The remote host closed the connection. The error code is 0x800704CD. Generated: Sun, 27 Nov 2016 16:33:58 GMT

System.Web.HttpException (0x800704CD): The remote host closed the connection. The error code is 0x800704CD. at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush() at System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async) at System.Web.HttpResponse.Flush() at System.Web.HttpResponseWrapper.Flush()

And the second one is:

System.Web.HttpException: Server cannot append header after HTTP headers have been sent. Generated: Sun, 27 Nov 2016 16:33:58 GMT

System.Web.HttpException (0x80004005): Server cannot append header after HTTP headers have been sent. at System.Web.HttpResponse.AppendHeader(String name, String value) at System.Web.HttpResponseWrapper.AppendHeader(String name, String value) at System.Web.Mvc.MvcHandler.AddVersionHeader(HttpContextBase httpContext) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)

Here is the code:

[HttpPost]
public async Task<EmptyResult> Export(ExcelViewModel vModel)
        {
            /*
             * Note:
             * Update the download date to keep track on new entries.
             */
            var fileName = string.Empty;
            var fileStream = await Task.Run(async () =>
            {
                var e = await _eventService.FindAsync(base.UserLanguageId, int.Parse(vModel.EventId));
                var orders = _orderSKUDiscountService.GetMembershipsByConfirmedOrders(base.AuthenticatedUser.Id, e.Slug, "");

                var lots = _lotService.GetLotByEventWihRecipient(e.Id).ToList();
                var stream = PushDataToExcel(lots, orders, DateTimeUtility.GetTimeFromUtc(e.LatestExcelDownloadDateUtc.Value));

                if (stream != null)
                {
                    e.LatestExcelDownloadDateUtc = DateTime.UtcNow;
                    if (await _unitOfWorkAsync.SaveChangesAsync() > 0)
                    {
                        fileName = Tools.StripAccent(e.Name) + "-rapport";
                        return stream;
                    }
                }

                return new MemoryStream();
            });

            // Write it back to the client
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", string.Format("attachment;  filename={0}.xlsx", fileName));
            Response.OutputStream.Write(fileStream.GetBuffer(), 0, (int)fileStream.Length);

            Response.Flush();
            Response.End();
            fileStream.Close();

            return new EmptyResult();
        }

To conclude, this is a MVC5 C# web application, host on Azure with a S0 database. I try to reproduce the problem by putting a long Tread.Sleep before and after the FindByAsync and GetMembershipsByConfirmedOrders you can see in the code above, which are the requests to the database, but I never got the Connection Timed Out. So I'm a little confused about how to solve it.

Thank you

David

c#
sql
asp.net-mvc
azure
connection-timeout

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0