.net long running httpwebrequest closing the connection? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Simulating connection problems for .NET HttpWebRequestHttpWebRequest not returning, connection closingDifference between decimal, float and double in .NET?Authentication headers not sending in HttpWebRequestHow to close underlying connections after catch httpwebrequest timeoutHow do I force my .NET application to run as administrator?Online exception:Unable to read data from the transport connection: An existing connection was forcibly closed by the remote hostC# Closing a HttpWebRequest DevDefinedHttpWebRequest, and self hosted REST web serviceHow to free connection after HttpWebRequest Async Abort()

Inverse square law not accurate for non-point masses?

Is this Kuo-toa homebrew race balanced?

Did John Wesley plagiarize Matthew Henry...?

Is the Mordenkainen's Sword spell underpowered?

Vertical ranges of Column Plots in 12

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

Is the time—manner—place ordering of adverbials an oversimplification?

The Nth Gryphon Number

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?

Was the pager message from Nick Fury to Captain Marvel unnecessary?

Is there a verb for listening stealthily?

How can I list files in reverse time order by a command and pass them as arguments to another command?

How to name indistinguishable henchmen in a screenplay?

Russian equivalents of おしゃれは足元から (Every good outfit starts with the shoes)

How do I say "this must not happen"?

How does the body cool itself in a stillsuit?

Does the Rock Gnome trait Artificer's Lore apply when you aren't proficient in History?

How to make triangles with rounded sides and corners? (squircle with 3 sides)

Can two people see the same photon?

How to infer difference of population proportion between two groups when proportion is small?

Keep at all times, the minus sign above aligned with minus sign below

Flight departed from the gate 5 min before scheduled departure time. Refund options

Table formatting with tabularx?



.net long running httpwebrequest closing the connection?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Simulating connection problems for .NET HttpWebRequestHttpWebRequest not returning, connection closingDifference between decimal, float and double in .NET?Authentication headers not sending in HttpWebRequestHow to close underlying connections after catch httpwebrequest timeoutHow do I force my .NET application to run as administrator?Online exception:Unable to read data from the transport connection: An existing connection was forcibly closed by the remote hostC# Closing a HttpWebRequest DevDefinedHttpWebRequest, and self hosted REST web serviceHow to free connection after HttpWebRequest Async Abort()



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















The goal is to make an http request to a service that could take over an hour to respond.



I created a httpwebrequest and sent the request body fine,
then I used:



var ar = BeginGetResponse(null, null);
ar.AsyncWaitHandle.WaitOne();


This is in the belief that the async call does not respect timeouts so I expect the request to wait forever for the server to respond.



On the server when it eventually has its response ready,
when it tries to write I get the exception "the remote host has closed the connection"



It looks like the client is still waiting at the WaitOne() but the connection has been closed.










share|improve this question






























    0















    The goal is to make an http request to a service that could take over an hour to respond.



    I created a httpwebrequest and sent the request body fine,
    then I used:



    var ar = BeginGetResponse(null, null);
    ar.AsyncWaitHandle.WaitOne();


    This is in the belief that the async call does not respect timeouts so I expect the request to wait forever for the server to respond.



    On the server when it eventually has its response ready,
    when it tries to write I get the exception "the remote host has closed the connection"



    It looks like the client is still waiting at the WaitOne() but the connection has been closed.










    share|improve this question


























      0












      0








      0








      The goal is to make an http request to a service that could take over an hour to respond.



      I created a httpwebrequest and sent the request body fine,
      then I used:



      var ar = BeginGetResponse(null, null);
      ar.AsyncWaitHandle.WaitOne();


      This is in the belief that the async call does not respect timeouts so I expect the request to wait forever for the server to respond.



      On the server when it eventually has its response ready,
      when it tries to write I get the exception "the remote host has closed the connection"



      It looks like the client is still waiting at the WaitOne() but the connection has been closed.










      share|improve this question
















      The goal is to make an http request to a service that could take over an hour to respond.



      I created a httpwebrequest and sent the request body fine,
      then I used:



      var ar = BeginGetResponse(null, null);
      ar.AsyncWaitHandle.WaitOne();


      This is in the belief that the async call does not respect timeouts so I expect the request to wait forever for the server to respond.



      On the server when it eventually has its response ready,
      when it tries to write I get the exception "the remote host has closed the connection"



      It looks like the client is still waiting at the WaitOne() but the connection has been closed.







      .net frameworks httpwebrequest httpresponse






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 3:50









      Dale Burrell

      3,50252755




      3,50252755










      asked Mar 5 at 15:04









      user2849221user2849221

      436




      436






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Finally sorted this I hope this saves others some time.
          I think the critical change was activating settcpkeepalive
          but I have mentioned other settings I changed just in case



          In IIS, using location tags in the web.config I set the executetimeout for the specifc ashx to 2 hours (thanks to Jon Hanna and Tomas Varacek for pointing this out) Not sure if this is absolutely necessary but if you have a process that takes up to 2 hours then it seems to make sense that the executetimeout needs to be at least 2 hours.



          The expect100continue in IIS is disabled.
          On the client side, the default for servicepoint expect100continue is true.
          I set it to false;



          I set the servicepoint maxidletime to 2 hours.
          Not sure if a connection waiting for a response is regarded as idle or not.
          I think this refers to a service point which is not associated with a connection, so probably not relevant to my problem



          I always use the begin/end pattern (async) versions where available
          especially beginread and endread. This is to avoid having to decipher the many timeouts on request/stream



          The default value of the service point settcpkeepalive is false
          I set the service_point SetTcpKeepAlive(true, 1000 * 60, 1000);
          I think this means that after 1 minute of idleness (waiting for the response) the client sends a keep alive "probe" to remind the server it is still waiting. If it does not receive a response to the probe it sends another after a second. Hopefully after it receives a response to the keep alive probe it returns to the 1 minute wait, but I am not sure. Note that there is a max of 10 probes which cannot be changed.



          Note that of course the timeout interval must be less than the connectionTimout configured for the site in IIS site advanced setting connectionlimits or else the connection will timeout before the first keep alive packet is sent.



          Another way I solved the problem was to accept that the system discourages open connections that are just waiting, so before kicking off the long running process, I start a task that does a response.Write every so often (1 minute).
          I flush the write and buffering is off on the response and nagling is disabled for the site and even though the message is small (1 byte) it does get all the way back to the client 3000 miles away)
          In this scenario:
          IIS does not regard the connection as idle, the httpwebrequest does not regard the connection as idle and the request keeps waiting just as I want it to.
          I have had this scheme running for several hours without problems



          Of course yet another way would be to have the server kick off the process and return immediately and have some periodic follow up requests poll the server to see if it is finished. Some would say this is more in the tradition of the request/response http methodology. I may do still do that but I wanted to have this option as a fallback if needed






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55005804%2fnet-long-running-httpwebrequest-closing-the-connection%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Finally sorted this I hope this saves others some time.
            I think the critical change was activating settcpkeepalive
            but I have mentioned other settings I changed just in case



            In IIS, using location tags in the web.config I set the executetimeout for the specifc ashx to 2 hours (thanks to Jon Hanna and Tomas Varacek for pointing this out) Not sure if this is absolutely necessary but if you have a process that takes up to 2 hours then it seems to make sense that the executetimeout needs to be at least 2 hours.



            The expect100continue in IIS is disabled.
            On the client side, the default for servicepoint expect100continue is true.
            I set it to false;



            I set the servicepoint maxidletime to 2 hours.
            Not sure if a connection waiting for a response is regarded as idle or not.
            I think this refers to a service point which is not associated with a connection, so probably not relevant to my problem



            I always use the begin/end pattern (async) versions where available
            especially beginread and endread. This is to avoid having to decipher the many timeouts on request/stream



            The default value of the service point settcpkeepalive is false
            I set the service_point SetTcpKeepAlive(true, 1000 * 60, 1000);
            I think this means that after 1 minute of idleness (waiting for the response) the client sends a keep alive "probe" to remind the server it is still waiting. If it does not receive a response to the probe it sends another after a second. Hopefully after it receives a response to the keep alive probe it returns to the 1 minute wait, but I am not sure. Note that there is a max of 10 probes which cannot be changed.



            Note that of course the timeout interval must be less than the connectionTimout configured for the site in IIS site advanced setting connectionlimits or else the connection will timeout before the first keep alive packet is sent.



            Another way I solved the problem was to accept that the system discourages open connections that are just waiting, so before kicking off the long running process, I start a task that does a response.Write every so often (1 minute).
            I flush the write and buffering is off on the response and nagling is disabled for the site and even though the message is small (1 byte) it does get all the way back to the client 3000 miles away)
            In this scenario:
            IIS does not regard the connection as idle, the httpwebrequest does not regard the connection as idle and the request keeps waiting just as I want it to.
            I have had this scheme running for several hours without problems



            Of course yet another way would be to have the server kick off the process and return immediately and have some periodic follow up requests poll the server to see if it is finished. Some would say this is more in the tradition of the request/response http methodology. I may do still do that but I wanted to have this option as a fallback if needed






            share|improve this answer





























              0














              Finally sorted this I hope this saves others some time.
              I think the critical change was activating settcpkeepalive
              but I have mentioned other settings I changed just in case



              In IIS, using location tags in the web.config I set the executetimeout for the specifc ashx to 2 hours (thanks to Jon Hanna and Tomas Varacek for pointing this out) Not sure if this is absolutely necessary but if you have a process that takes up to 2 hours then it seems to make sense that the executetimeout needs to be at least 2 hours.



              The expect100continue in IIS is disabled.
              On the client side, the default for servicepoint expect100continue is true.
              I set it to false;



              I set the servicepoint maxidletime to 2 hours.
              Not sure if a connection waiting for a response is regarded as idle or not.
              I think this refers to a service point which is not associated with a connection, so probably not relevant to my problem



              I always use the begin/end pattern (async) versions where available
              especially beginread and endread. This is to avoid having to decipher the many timeouts on request/stream



              The default value of the service point settcpkeepalive is false
              I set the service_point SetTcpKeepAlive(true, 1000 * 60, 1000);
              I think this means that after 1 minute of idleness (waiting for the response) the client sends a keep alive "probe" to remind the server it is still waiting. If it does not receive a response to the probe it sends another after a second. Hopefully after it receives a response to the keep alive probe it returns to the 1 minute wait, but I am not sure. Note that there is a max of 10 probes which cannot be changed.



              Note that of course the timeout interval must be less than the connectionTimout configured for the site in IIS site advanced setting connectionlimits or else the connection will timeout before the first keep alive packet is sent.



              Another way I solved the problem was to accept that the system discourages open connections that are just waiting, so before kicking off the long running process, I start a task that does a response.Write every so often (1 minute).
              I flush the write and buffering is off on the response and nagling is disabled for the site and even though the message is small (1 byte) it does get all the way back to the client 3000 miles away)
              In this scenario:
              IIS does not regard the connection as idle, the httpwebrequest does not regard the connection as idle and the request keeps waiting just as I want it to.
              I have had this scheme running for several hours without problems



              Of course yet another way would be to have the server kick off the process and return immediately and have some periodic follow up requests poll the server to see if it is finished. Some would say this is more in the tradition of the request/response http methodology. I may do still do that but I wanted to have this option as a fallback if needed






              share|improve this answer



























                0












                0








                0







                Finally sorted this I hope this saves others some time.
                I think the critical change was activating settcpkeepalive
                but I have mentioned other settings I changed just in case



                In IIS, using location tags in the web.config I set the executetimeout for the specifc ashx to 2 hours (thanks to Jon Hanna and Tomas Varacek for pointing this out) Not sure if this is absolutely necessary but if you have a process that takes up to 2 hours then it seems to make sense that the executetimeout needs to be at least 2 hours.



                The expect100continue in IIS is disabled.
                On the client side, the default for servicepoint expect100continue is true.
                I set it to false;



                I set the servicepoint maxidletime to 2 hours.
                Not sure if a connection waiting for a response is regarded as idle or not.
                I think this refers to a service point which is not associated with a connection, so probably not relevant to my problem



                I always use the begin/end pattern (async) versions where available
                especially beginread and endread. This is to avoid having to decipher the many timeouts on request/stream



                The default value of the service point settcpkeepalive is false
                I set the service_point SetTcpKeepAlive(true, 1000 * 60, 1000);
                I think this means that after 1 minute of idleness (waiting for the response) the client sends a keep alive "probe" to remind the server it is still waiting. If it does not receive a response to the probe it sends another after a second. Hopefully after it receives a response to the keep alive probe it returns to the 1 minute wait, but I am not sure. Note that there is a max of 10 probes which cannot be changed.



                Note that of course the timeout interval must be less than the connectionTimout configured for the site in IIS site advanced setting connectionlimits or else the connection will timeout before the first keep alive packet is sent.



                Another way I solved the problem was to accept that the system discourages open connections that are just waiting, so before kicking off the long running process, I start a task that does a response.Write every so often (1 minute).
                I flush the write and buffering is off on the response and nagling is disabled for the site and even though the message is small (1 byte) it does get all the way back to the client 3000 miles away)
                In this scenario:
                IIS does not regard the connection as idle, the httpwebrequest does not regard the connection as idle and the request keeps waiting just as I want it to.
                I have had this scheme running for several hours without problems



                Of course yet another way would be to have the server kick off the process and return immediately and have some periodic follow up requests poll the server to see if it is finished. Some would say this is more in the tradition of the request/response http methodology. I may do still do that but I wanted to have this option as a fallback if needed






                share|improve this answer















                Finally sorted this I hope this saves others some time.
                I think the critical change was activating settcpkeepalive
                but I have mentioned other settings I changed just in case



                In IIS, using location tags in the web.config I set the executetimeout for the specifc ashx to 2 hours (thanks to Jon Hanna and Tomas Varacek for pointing this out) Not sure if this is absolutely necessary but if you have a process that takes up to 2 hours then it seems to make sense that the executetimeout needs to be at least 2 hours.



                The expect100continue in IIS is disabled.
                On the client side, the default for servicepoint expect100continue is true.
                I set it to false;



                I set the servicepoint maxidletime to 2 hours.
                Not sure if a connection waiting for a response is regarded as idle or not.
                I think this refers to a service point which is not associated with a connection, so probably not relevant to my problem



                I always use the begin/end pattern (async) versions where available
                especially beginread and endread. This is to avoid having to decipher the many timeouts on request/stream



                The default value of the service point settcpkeepalive is false
                I set the service_point SetTcpKeepAlive(true, 1000 * 60, 1000);
                I think this means that after 1 minute of idleness (waiting for the response) the client sends a keep alive "probe" to remind the server it is still waiting. If it does not receive a response to the probe it sends another after a second. Hopefully after it receives a response to the keep alive probe it returns to the 1 minute wait, but I am not sure. Note that there is a max of 10 probes which cannot be changed.



                Note that of course the timeout interval must be less than the connectionTimout configured for the site in IIS site advanced setting connectionlimits or else the connection will timeout before the first keep alive packet is sent.



                Another way I solved the problem was to accept that the system discourages open connections that are just waiting, so before kicking off the long running process, I start a task that does a response.Write every so often (1 minute).
                I flush the write and buffering is off on the response and nagling is disabled for the site and even though the message is small (1 byte) it does get all the way back to the client 3000 miles away)
                In this scenario:
                IIS does not regard the connection as idle, the httpwebrequest does not regard the connection as idle and the request keeps waiting just as I want it to.
                I have had this scheme running for several hours without problems



                Of course yet another way would be to have the server kick off the process and return immediately and have some periodic follow up requests poll the server to see if it is finished. Some would say this is more in the tradition of the request/response http methodology. I may do still do that but I wanted to have this option as a fallback if needed







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 9 at 23:48

























                answered Mar 6 at 17:03









                user2849221user2849221

                436




                436





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55005804%2fnet-long-running-httpwebrequest-closing-the-connection%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко