471,325 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

File.OpenRead and System.IO.IOException...

I'm using a File.OpenRead call within a custom httphandler to read a
file from a remote computer using a unc path as the parameter to
OpenRead. This is done within an impersonation call to ensure the
proper credentials are used to get the remote file. After a few page
refreshes (approx 3-5), I get this exception thrown:

[System.IO.IOException] = {"No more connections can be made to this
remote computer at this time because there are already as many
connections as the computer can accept.\r\n"}

Is there any way to avoid getting this error, or a better way to
implement this?

Thanks in advance,
Kevin

Dec 8 '05 #1
7 10807
Kevin,

Are you properly disposing of the windows impersonation contexts, as
well as the file streams? If not, this could be the cause of the problem.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kevin" <kd******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I'm using a File.OpenRead call within a custom httphandler to read a
file from a remote computer using a unc path as the parameter to
OpenRead. This is done within an impersonation call to ensure the
proper credentials are used to get the remote file. After a few page
refreshes (approx 3-5), I get this exception thrown:

[System.IO.IOException] = {"No more connections can be made to this
remote computer at this time because there are already as many
connections as the computer can accept.\r\n"}

Is there any way to avoid getting this error, or a better way to
implement this?

Thanks in advance,
Kevin

Dec 8 '05 #2
Yes, the code is below, I believe this is the correct way to dispose
of them:

....

System.IO.FileStream fs = File.OpenRead(<UNC Path>);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
ctx.Response.BinaryWrite(bytes);

fs.Close();
fs.Dispose();

....

Dec 8 '05 #3
Kevin,

This is not a good way to do this. If you have an exception, then the
file handles will not close until they are garbage collected.

Also, how are you impersonating the credentials? Are you making a call
to the Impersonate method on the WindowsIdentity class? If so, you need to
call Dispose on that as well.

Your code (without the impersonation call), should look like this:

using (System.IO.FileStream fs = File.OpenRead(<UNC Path>))
{
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
ctx.Response.BinaryWrite(bytes);
}

Also, it should be noted that you aren't guaranteed to get all of the
bytes read in one call to Read. Jon Skeet has a good item in his FAQ
regarding this:

http://www.yoda.arachsys.com/csharp/readbinary.html
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kevin" <kd******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Yes, the code is below, I believe this is the correct way to dispose
of them:

...

System.IO.FileStream fs = File.OpenRead(<UNC Path>);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
ctx.Response.BinaryWrite(bytes);

fs.Close();
fs.Dispose();

...

Dec 8 '05 #4
Thank you for your help.

Currently, to impersonate, we have wrapped the LogonUser win32 api into
a c# class, and I use that. This does have an Undo method which I am
calling when everything is done, or if any exceptions occur.

I changed the code to what you suggested and also used Jon Skeets
method for correctly reading in the binary data.

However, the problem still exists.

Thanks,
Kevin

Dec 8 '05 #5
Kevin,

Can you show the code which does the impersonation which wraps this
code?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kevin" <kd******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thank you for your help.

Currently, to impersonate, we have wrapped the LogonUser win32 api into
a c# class, and I use that. This does have an Undo method which I am
calling when everything is done, or if any exceptions occur.

I changed the code to what you suggested and also used Jon Skeets
method for correctly reading in the binary data.

However, the problem still exists.

Thanks,
Kevin

Dec 8 '05 #6

"Kevin" <kd******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thank you for your help.

Currently, to impersonate, we have wrapped the LogonUser win32 api into
a c# class, and I use that. This does have an Undo method which I am
calling when everything is done, or if any exceptions occur.

I changed the code to what you suggested and also used Jon Skeets
method for correctly reading in the binary data.

However, the problem still exists.

Thanks,
Kevin


Keep in mind that the SMB redirector keeps the session open for at least 10
seconds after the last file for the session was closed, that means that you
will exhaust the connection limit if you are reconnecting (LogonUser -
Impersonate- Open file) more than the connection limit value set for the
share within that period.
Anyway, your remote share access code should be running in a COM+ server
type application, running with fixed credentials. This application could
have the remote share permanently mapped, as such eleminating the
Authentication/Authorization and Server connect overhead.

Willy.
Dec 8 '05 #7
Thank you, I believe this is whats happening, at least thats how it
appears. Would running the http handler as that same fixed credential
solve the problem? I guess what I'm asking, is that if the user the
http handler runs as, already has permissions to read the file from the
unc path, would I still run into this problem? I'm thinking that I
would, since it would still be using the SMB redirector.

Thanks,
Kevin

Dec 11 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by R Reyes | last post: by
8 posts views Thread by Gabe Moothart | last post: by
3 posts views Thread by curt.bathras | last post: by
8 posts views Thread by Jromero | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.