Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HttpWebRequest and Reusing Connections

Reply
Thread Tools

HttpWebRequest and Reusing Connections

 
 
Mike
Guest
Posts: n/a
 
      06-09-2010
Hi, I need to modify an existing Windows forms application that (among
many other things) uploads files from the users machine to the web
server which is on the same domain. On the server there is a "Generic
Handler" / .ashx that receive the files. The current implementation
uploads files in one shot as a single byte array, which for very large
files (100's of MBs) is prone to OutOfMemoryExceptions anywhere in the
process, both client and server side. So instead we need to upload the
files in more manageable "chunks".

The code below reads and uploads files 64 KB at a time. It works
except for one big problem. As it's running, it appears from netstat
output that each HttpWebRequest ties up a distinct port on the client
with a status of TIME_WAIT. After about 4,000 or so requests I get
this error on the client:

WebException was unhandled
Unable to connect to the remote server
Only one usage of each socket address (protocol/network address/port)
is normally permitted

So this error message, together with netstat, indicates all the ports
on the client machine are tied up. I would expect that rather than
creating new connections for each request they would be reused based
on the post below, but evidently they aren't:

http://social.msdn.microsoft.com/For...d-1c8b9d2ab42b

Any suggestions how I can either release the port immediately after
the request/response completes, or even better reuse the same port for
all of the requests since they are serial/synchronous? To that end,
I've tried using various combinations of KeepAlive,
ConnectionGroupName, UnsafeAuthenticatedConnectionSharing, and others
to force reuse based on other posts I've read, but I wasn't able to
hit on the "magic" combination.

Also, I've read about editing the registry to increase the number of
ports available to the application, but in production I cannot do this
on the user's machines, and besides that doesn't really seem like a
"solution" to me.

Thanks!
MJ

Code:
private void TestUpload()
{
  UploadFile(@"C:\Temp\My800MBFile.dat", "http://localhost:1776/
UploadFile.ashx?fileId=" + Guid.NewGuid().ToString());
}

private void UploadFile(string fileName, string url)
{
  using (FileStream fileStream = new FileStream(fileName,
FileMode.Open, FileAccess.Read))
  {
    byte[] buffer = new byte[65536];

    int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
    while (bytesRead > 0)
    {
      HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
      request.Method = "POST";

      using (Stream requestStream = request.GetRequestStream())
      {
        requestStream.Write(buffer, 0, bytesRead);
        requestStream.Close();
      }

      using (HttpWebResponse response = (HttpWebResponse)
request.GetResponse())
      {
        response.Close();
      }

      bytesRead = fileStream.Read(buffer, 0, buffer.Length);
    }

    fileStream.Close();
  }
}
 
Reply With Quote
 
 
 
 
cubaman
Guest
Posts: n/a
 
      06-11-2010
On Jun 10, 1:01*am, Mike <(E-Mail Removed)> wrote:
> Hi, I need to modify an existing Windows forms application that (among
> many other things) uploads files from the users machine to the web
> server which is on the same domain. On the server there is a "Generic
> Handler" / .ashx that receive the files. The current implementation
> uploads files in one shot as a single byte array, which for very large
> files (100's of MBs) is prone to OutOfMemoryExceptions anywhere in the
> process, both client and server side. So instead we need to upload the
> files in more manageable "chunks".
>
> The code below reads and uploads files 64 KB at a time. It works
> except for one big problem. As it's running, it appears from netstat
> output that each HttpWebRequest ties up a distinct port on the client
> with a status of TIME_WAIT. After about 4,000 or so requests I get
> this error on the client:
>
> WebException was unhandled
> Unable to connect to the remote server
> Only one usage of each socket address (protocol/network address/port)
> is normally permitted
>
> So this error message, together with netstat, indicates all the ports
> on the client machine are tied up. I would expect that rather than
> creating new connections for each request they would be reused based
> on the post below, but evidently they aren't:
>
> http://social.msdn.microsoft.com/For...d/6d67ff8b-14f...
>
> Any suggestions how I can either release the port immediately after
> the request/response completes, or even better reuse the same port for
> all of the requests since they are serial/synchronous? To that end,
> I've tried using various combinations of KeepAlive,
> ConnectionGroupName, UnsafeAuthenticatedConnectionSharing, and others
> to force reuse based on other posts I've read, but I wasn't able to
> hit on the "magic" combination.
>
> Also, I've read about editing the registry to increase the number of
> ports available to the application, but in production I cannot do this
> on the user's machines, and besides that doesn't really seem like a
> "solution" to me.
>
> Thanks!
> MJ
>
>
Code:
>
> private void TestUpload()
> {
> * UploadFile(@"C:\Temp\My800MBFile.dat", "http://localhost:1776/
> UploadFile.ashx?fileId=" + Guid.NewGuid().ToString());
>
> }
>
> private void UploadFile(string fileName, string url)
> {
> * using (FileStream fileStream = new FileStream(fileName,
> FileMode.Open, FileAccess.Read))
> * {
> * * byte[] buffer = new byte[65536];
>
> * * int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
> * * while (bytesRead > 0)
> * * {
> * * * HttpWebRequest request = (HttpWebRequest)
> WebRequest.Create(url);
> * * * request.Method = "POST";
>
> * * * using (Stream requestStream = request.GetRequestStream())
> * * * {
> * * * * requestStream.Write(buffer, 0, bytesRead);
> * * * * requestStream.Close();
> * * * }
>
> * * * using (HttpWebResponse response = (HttpWebResponse)
> request.GetResponse())
> * * * {
> * * * * response.Close();
> * * * }
>
> * * * bytesRead = fileStream.Read(buffer, 0, buffer.Length);
> * * }
>
> * * fileStream.Close();
> * }
>
> }
>
>


First, I would create a WCF Service instead of a ashx to handle the
file upload. Second, i'd try to implement some kind of compression
before uploading the file.
Sorry, cant help with connection re-use.
Best regards.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lingering Idle Oracle Connections? Persistent connections? jobs ASP .Net 2 11-16-2007 03:37 AM
.Net 2.0 : Soap client : reusing http connections ? Steve B. ASP .Net 3 07-17-2007 04:04 PM
.Net 2.0 : Soap client : reusing http connections ? Steve B. ASP .Net Web Services 3 07-17-2007 04:04 PM
Reusing HttpWebRequest.GetRequestStream Dan ASP .Net 4 06-01-2007 07:29 AM
xmlprclib/server not reusing connections Roger Binns Python 14 02-26-2004 04:38 PM



Advertisments