0
votes

I am using this code to read a file using web services,

FileStream stream = File.OpenRead(FileName);
byte[] contents = new byte[stream.Length];
stream.Read(contents, 0, (int)stream.Length);
stream.Close();

but I get this error

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> Cannot open file "fileName.pdf". ---> Cannot open file "filename.pdf". at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) at

Its random as well, sometimes it through error sometimes it doesn't.

Code is being used by 200+ users and it only throws exception randomly.

2
filename.pdf does not exist? Or at least not in the directory that the service is looking in? Try to include the entire path to the file. - D Stanley
Does this exception have an inner exception? - Geeky Guy
@DStanley it happens randomly... and works next moment. - Mathematics
Did you tried File.Exists(path)? - Butzke
@Renan Nope code is on production server, no other exceptions - Mathematics

2 Answers

0
votes

Excuse my ignorance of web services' HTTP implementation, it may account for this but if you are handling requests concurrently then two requests at the same time could potentially cause file contention.

You could potentially address this with a lock(obj){ } around the code or some more complex marshalling.

0
votes

Open the file using shared access using this method

public static FileStream Open(
    string path,
    FileMode mode,
    FileAccess access,
    FileShare share
)

EDIT ADDED

Just to be sure, any place you can open this file, you need to open it shared. If anything outsize the web server can be opening the file (and you can't make it share read-only too). You can likely work-around the problem by trapping the exception when opening the file, sleep for 100 msec (or some other small amount) and retry up to nn time. This is not a good solution, just a hack to work-around the problem as your will tie up your server thread will you do this. But, if this hack works, you will know that this is the actual problem (file contention)