<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Shimon Krokhmal's blog - Web services</title>
    <link>http://www.krokhmal.com/</link>
    <description>medium : .NET | JavaScript | Secure coding | Databases | Sql Server | Oracle | CodeSmith | SPS | Life</description>
    <language>en-us</language>
    <copyright>Shimon Krokhmal</copyright>
    <lastBuildDate>Sun, 30 Sep 2007 14:30:05 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>Shimonkr@gmail.com</managingEditor>
    <webMaster>Shimonkr@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=f615b4b3-eb77-4534-bddd-daeb7c6553ce</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,f615b4b3-eb77-4534-bddd-daeb7c6553ce.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,f615b4b3-eb77-4534-bddd-daeb7c6553ce.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f615b4b3-eb77-4534-bddd-daeb7c6553ce</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
recently i encountered some errors with web services regarding webService response.
</p>
        <p>
the full Exception that was thown was :<br />
"<strong>Server was unable to process request. -&gt; the underlying connection was
closed: A connection that was expected to be kept alive was closed by the server</strong>"
</p>
        <p>
hmm, not a very nice exception.<br />
digging a little in , i found few reasons that may cause that error.
</p>
        <ul>
          <li>
idle time on the client side is greater than on the server side. 
</li>
          <li>
network issues that prevent the Keep-Alive feature from being committed. (firewalls,
proxies and so...).</li>
        </ul>
        <p>
what is this KeepAlive feature :<br />
this "great" feature actually keeps the connection between the WebService client and
server open for a defined amount of time,<br />
thus enhancing WebService interaction performance by eliminating the need to open
a new connection each time.
</p>
        <p>
unfortunately, the time for that window is not synchronized between the client and
the server.<br />
this situation creates a problem, on which the client may think that the connection
is still open, but the server closed it.
</p>
        <p>
so, what can be done so solve that matter ?
</p>
        <ul>
          <li>
ensure the reasons above would not happen 
</li>
          <li>
disable the KeepAlive feature.</li>
        </ul>
        <p>
since, ensuring that our time is not greater than the one on the server side is impossible
when dealing with 3rd party services,<br />
not even mentioning the 3rd party network configuration, the optimal solution for
this issue will be disabling the Keep-Alive feature.
</p>
        <p>
          <strong>how can we do that ?</strong>
        </p>
        <p>
when adding a web reference with visual studio 2005, it automatically generates a
file called reference.cs which is used as a proxy to the web service on the server.<br />
this is the place which we should do the "fix".
</p>
        <p>
there are 2 ways to prevent this certain problem from happening :
</p>
        <ul>
          <li>
set the KeepAlive property to false 
</li>
          <li>
use the HTTP protocol version 1.0 (<a href="http://www.research.att.com/~bala/papers/h0vh1.html">difference
between http 1.0 and 1.1</a>)</li>
        </ul>
        <p>
since our "proxy" is being generated each time we update our web reference, we would
not want the change to be made in the reference.cs file, since it can be overridden
the next time we will do "update web reference"
</p>
        <p>
what we can do, is use the "partial class" feature that presented in .Net 2.0 framework
to solve this issue.
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> theNamespaceOfYourServiceAsStatedInTheReferenceFile<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> partial <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> MyService<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
this method overides the base getWebRequest,</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
thus preventing the KeepAlive feature</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="uri"&gt;the given uri&lt;/param&gt;</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;the needed web request with the disabled keep alive feature&lt;/returns&gt;</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span> System.Net.WebRequest
GetWebRequest(Uri uri)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Do the base class operation and obtain the specific web request</span><br />
System.Net.HttpWebRequest webRequest <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (System.Net.HttpWebRequest)<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>.GetWebRequest(uri);<br /><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
do the keep alive handling</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> Utils.DisableKeepAlive(webRequest);<br />
}<br />
}</span>
        </p>
        <p>
now we need to decide how we are going the disable it(write the DisableKeepAlive in
our utils)
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method should disable the keep alive feature</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="w"&gt;the needed HttpWebRequest&lt;/param&gt;</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;Disabled HttpWebRequest&lt;/returns&gt;</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span> HttpWebRequest
DisableKeepAlive(HttpWebRequest w)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Set the keep-Alive to false, thus for not using it</span><br />
w.KeepAlive <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">false</span>;<br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
the second way</span><br />
w.ProtocolVersion <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> System.Net.HttpVersion.Version10;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> w;<br />
}</span>
        </p>
        <p>
thats it, now we have successfully disabled the KeepAlive feature<br /></p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=f615b4b3-eb77-4534-bddd-daeb7c6553ce" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Web services - The underlying connection was closed </title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,f615b4b3-eb77-4534-bddd-daeb7c6553ce.aspx</guid>
      <link>http://www.krokhmal.com/2007/09/30/WebServicesTheUnderlyingConnectionWasClosed.aspx</link>
      <pubDate>Sun, 30 Sep 2007 14:30:05 GMT</pubDate>
      <description>&lt;p&gt;
recently i encountered some errors with web services regarding webService response.
&lt;/p&gt;
&lt;p&gt;
the full Exception that was thown was :&lt;br&gt;
"&lt;strong&gt;Server was unable to process request. -&amp;gt; the underlying connection was
closed: A connection that was expected to be kept alive was closed by the server&lt;/strong&gt;"
&lt;/p&gt;
&lt;p&gt;
hmm, not a very nice exception.&lt;br&gt;
digging a little in , i found few reasons that may cause that error.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
idle time on the client side is greater than on the server side. 
&lt;li&gt;
network issues that prevent the Keep-Alive feature from being committed. (firewalls,
proxies and so...).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
what is this KeepAlive feature :&lt;br&gt;
this "great" feature actually keeps the connection between the WebService client and
server open for a defined amount of time,&lt;br&gt;
thus enhancing WebService interaction performance by eliminating the need to open
a new connection each time.
&lt;/p&gt;
&lt;p&gt;
unfortunately, the time for that window is not synchronized between the client and
the server.&lt;br&gt;
this situation creates a problem, on which the client may think that the connection
is still open, but the server closed it.
&lt;/p&gt;
&lt;p&gt;
so, what can be done so solve that matter ?
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
ensure the reasons above would not happen 
&lt;li&gt;
disable the KeepAlive feature.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
since, ensuring that our time is not greater than the one on the server side is impossible
when dealing with 3rd party services,&lt;br&gt;
not even mentioning the 3rd party network configuration, the optimal solution for
this issue will be disabling the Keep-Alive feature.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;how can we do that ?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
when adding a web reference with visual studio 2005, it automatically generates a
file called reference.cs which is used as a proxy to the web service on the server.&lt;br&gt;
this is the place which we should do the "fix".
&lt;/p&gt;
&lt;p&gt;
there are 2 ways to prevent this certain problem from happening :
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
set the KeepAlive property to false 
&lt;li&gt;
use the HTTP protocol version 1.0 (&lt;a href="http://www.research.att.com/~bala/papers/h0vh1.html"&gt;difference
between http 1.0 and 1.1&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
since our "proxy" is being generated each time we update our web reference, we would
not want the change to be made in the reference.cs file, since it can be overridden
the next time we will do "update web reference"
&lt;/p&gt;
&lt;p&gt;
what we can do, is use the "partial class" feature that presented in .Net 2.0 framework
to solve this issue.
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; theNamespaceOfYourServiceAsStatedInTheReferenceFile&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; partial &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; MyService&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
this method overides the base getWebRequest,&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
thus preventing the KeepAlive feature&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="uri"&amp;gt;the given uri&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;the needed web request with the disabled keep alive feature&amp;lt;/returns&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; System.Net.WebRequest
GetWebRequest(Uri uri)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Do the base class operation and obtain the specific web request&lt;/span&gt;
&lt;br&gt;
System.Net.HttpWebRequest webRequest &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (System.Net.HttpWebRequest)&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;.GetWebRequest(uri);&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
do the keep alive handling&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; Utils.DisableKeepAlive(webRequest);&lt;br&gt;
}&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
now we need to decide how we are going the disable it(write the DisableKeepAlive in
our utils)
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method should disable the keep alive feature&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="w"&amp;gt;the needed HttpWebRequest&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;Disabled HttpWebRequest&amp;lt;/returns&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; HttpWebRequest
DisableKeepAlive(HttpWebRequest w)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Set the keep-Alive to false, thus for not using it&lt;/span&gt;
&lt;br&gt;
w.KeepAlive &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;false&lt;/span&gt;;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
the second way&lt;/span&gt;
&lt;br&gt;
w.ProtocolVersion &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; System.Net.HttpVersion.Version10;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; w;&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
thats it, now we have successfully disabled the KeepAlive feature&lt;br&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=f615b4b3-eb77-4534-bddd-daeb7c6553ce" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,f615b4b3-eb77-4534-bddd-daeb7c6553ce.aspx</comments>
      <category>.Net 2.0</category>
      <category>.Net/C#</category>
      <category>Web services</category>
    </item>
  </channel>
</rss>