ASP.net host closed the connection

6

I am trying to stop this warning in IIS and I am reading that I should check this object IsClientConnected before I call TransmitFile(filename) . Is this correct or is Another way to correct this ?

IIS exception

Exception information: Exception type: HttpException Exception message: The remote host closed the connection. The error code is 0x800703E3. at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean ?throwOnDisconnect)

    if (context.Response.IsClientConnected)
    {
        context.Response.Clear();
        context.Response.ClearContent();
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.AddHeader("Expires", "-1");
        context.Response.ContentType = "application/pdf";
        context.Response.TransmitFile(filename);
        context.Response.Flush();
        context.Response.End();
    }
    else
    {
        context.Response.End();
    }

updated Code

            try
            {

                context.Response.Clear();
                context.Response.ClearContent();
                context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                context.Response.AddHeader("Expires", "-1");
                context.Response.ContentType = "application/pdf";
                context.Response.TransmitFile(filename);
                context.ApplicationInstance.CompleteRequest()
                //context.Response.Flush();
                //context.Response.End();
            }
            catch (HttpException hex)
            {

            }
c#
asp.net
asked on Stack Overflow Jun 1, 2020 by Jefferson • edited Jun 6, 2020 by Jefferson

1 Answer

2

This error implies that there is a Runtime Exception in your code. Usually, when hosting your app through the IIS there are such errors which will close the connection immediately.

You should debug your code by attaching to the process w3wp.

By navigating in Visual Studio to:

---> DEBUG
   ---> Attach to process
   ---> choose w3wp.exe
   ---> attach to process

The troubleshooter implies you to check the IsClientConnected object because it is the most common issue that will likely to cause the exception - if you are trying to call the client which does not exists (IsClientConnected is null for example).

answered on Stack Overflow Jun 7, 2020 by Barr J • edited Jul 11, 2020 by marc_s

User contributions licensed under CC BY-SA 3.0