locked
Response.IsClientConnected dont work correctily on iis 7.5 RRS feed

  • Question

  • User-1847302843 posted

    Hi friend

    Here is my code, let me explain with code

    for (int i = 0; i < maxCount && Response.IsClientConnected; i++)
            {
    
                buffer = _BinaryReader.ReadBytes(readLen);
                Response.OutputStream.Write(buffer, 0, buffer.Length);
                Response.OutputStream.Flush();
                LockAndUpdateTraffic(FileID, buffer.Length);
    
            }

    first I check whether client is connected or not but alwayes IsClientConnected return true even if client is disconnected

    my Response.OutputStream.Write is completely done an no exception occure when client doesnt read stream server continuesly write data to it

    when I am debugging on visual studio 2008 everything is ok but when I run my application on IIS 7.5 hosted on windows 2008 R2 this problem occured

    I Need to measure how many byte I sent to client but with this problem My result is all bytes have been sent because OutputStream.Write dont wait for client to read

    Monday, June 30, 2014 12:48 PM

Answers

  • User281315223 posted

    The actual IsClientConnection method may not work or be providing your with exactly what you are looking for. Based on the documentation, the IsClientConnected property returns false when the following conditions are true:

    • The connection to the client was terminated. This can occur if the Close method was invoked, or if the client stopped execution of the Web page or browsed to another page.
    • The HttpWorkerRequest object that is handling the request is null or the HttpWorkerRequest.IsClientConnected method returns false. If a custom HttpWorkerRequest object handles the request, then the HttpWorkerRequest.IsClientConnected method might be set based on custom criteria. For example, the custom worker request might force a time-out after a period of time.

    So basically, it is going to return true unless both of the above conditions are met (which may be a part of the issue that you are encountering). Is there any case that your connection may be timing out? How / Where are currently calling this above method from?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2014 12:56 PM
  • User281315223 posted

    You likely will need to update your maxRequestLength within your web.config to handle files and uploads that are large (as the default limit is often 4MB). This can be handled within the <system.web> section of your web.config or the <system.webServer> section if you want to handle it at the IIS level (both are probably a good idea). The odds are that your item is exceeding this size and triggering an exception that might be resulting in the issues you are experiencing.

    It's important to know that maxAllowedContentLength is measured in bytes and maximumRequestLength is measured in kilobytes when settings these values so you'll need to adjust them accordingly if you plan on handling much larger files : 

    <configuration>
        <system.web>
            <!-- This will handle requests up to 1024MB (1GB) -->
            <httpRuntime maxRequestLength="1048576" timeout="3600" />
        </system.web>
    </configuration>
    
    <!-- IIS Specific Targeting (noted by the system.webServer section) -->
    <system.webServer>
       <security>
          <requestFiltering>
             <!-- This will handle requests up to 1024MB (1GB) -->
             <requestLimits maxAllowedContentLength="1048576000" />
          </requestFiltering>
       </security>
    </system.webServer>

    If you wanted to use 50MB as a limit, you would need to change the values to the following respectively : 

    <configuration>
        <system.web>
            <!-- This will handle requests up to 50MB -->
            <httpRuntime maxRequestLength="51200" timeout="3600" />
        </system.web>
    </configuration>
    
    <!-- IIS Specific Targeting (noted by the system.webServer section) -->
    <system.webServer>
       <security>
          <requestFiltering>
             <!-- This will handle requests up to 50MB -->
             <requestLimits maxAllowedContentLength="52428800" />
          </requestFiltering>
       </security>
    </system.webServer>

    If you don't see these sections within your existing web.config file, you'll simply need to copy them in.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2014 4:13 PM

All replies

  • User281315223 posted

    The actual IsClientConnection method may not work or be providing your with exactly what you are looking for. Based on the documentation, the IsClientConnected property returns false when the following conditions are true:

    • The connection to the client was terminated. This can occur if the Close method was invoked, or if the client stopped execution of the Web page or browsed to another page.
    • The HttpWorkerRequest object that is handling the request is null or the HttpWorkerRequest.IsClientConnected method returns false. If a custom HttpWorkerRequest object handles the request, then the HttpWorkerRequest.IsClientConnected method might be set based on custom criteria. For example, the custom worker request might force a time-out after a period of time.

    So basically, it is going to return true unless both of the above conditions are met (which may be a part of the issue that you are encountering). Is there any case that your connection may be timing out? How / Where are currently calling this above method from?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2014 12:56 PM
  • User-1847302843 posted

    I check this property on pageload

    let me explain my scenario

    my scenario on client is like this

    client is download manager

    client get file stream detect file size and split file to 8 part

    part one contiues current stream (let called it Stream A) for 1/8 size of file

    and other part create stream with range header 1/8 of file size

    my problem on server occure on response to Stream A

    when Stream A completely download 1/8 file the stream will be disconnected but server continue writing byte to client stream until end of file reached

    and the point is when I am on vs 2008 the scenario is work fine but when application published on iis 7.5 this problem occure

    Monday, June 30, 2014 1:27 PM
  • User-1847302843 posted

    I found that response after writing 17 MB to client detect isclientconnected is false how can I decrease this size?

    Monday, June 30, 2014 1:59 PM
  • User281315223 posted

    You likely will need to update your maxRequestLength within your web.config to handle files and uploads that are large (as the default limit is often 4MB). This can be handled within the <system.web> section of your web.config or the <system.webServer> section if you want to handle it at the IIS level (both are probably a good idea). The odds are that your item is exceeding this size and triggering an exception that might be resulting in the issues you are experiencing.

    It's important to know that maxAllowedContentLength is measured in bytes and maximumRequestLength is measured in kilobytes when settings these values so you'll need to adjust them accordingly if you plan on handling much larger files : 

    <configuration>
        <system.web>
            <!-- This will handle requests up to 1024MB (1GB) -->
            <httpRuntime maxRequestLength="1048576" timeout="3600" />
        </system.web>
    </configuration>
    
    <!-- IIS Specific Targeting (noted by the system.webServer section) -->
    <system.webServer>
       <security>
          <requestFiltering>
             <!-- This will handle requests up to 1024MB (1GB) -->
             <requestLimits maxAllowedContentLength="1048576000" />
          </requestFiltering>
       </security>
    </system.webServer>

    If you wanted to use 50MB as a limit, you would need to change the values to the following respectively : 

    <configuration>
        <system.web>
            <!-- This will handle requests up to 50MB -->
            <httpRuntime maxRequestLength="51200" timeout="3600" />
        </system.web>
    </configuration>
    
    <!-- IIS Specific Targeting (noted by the system.webServer section) -->
    <system.webServer>
       <security>
          <requestFiltering>
             <!-- This will handle requests up to 50MB -->
             <requestLimits maxAllowedContentLength="52428800" />
          </requestFiltering>
       </security>
    </system.webServer>

    If you don't see these sections within your existing web.config file, you'll simply need to copy them in.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2014 4:13 PM