Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net host closed the connection

Tags:

c#

asp.net

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)
            {

            }
like image 969
Jefferson Avatar asked Jun 01 '20 19:06

Jefferson


Video Answer


1 Answers

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).

like image 69
Barr J Avatar answered Sep 28 '22 08:09

Barr J