Click here to Skip to main content
15,889,730 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am not sure if this is the place to ask. It may be a simple error. But I have a webservice that calls filestream writer.

I have this in the beggining of my constructor. If tracing is set to true, then it changes the instance variables fs and w to those.
C#
if (Properties.Settings.Default.Trace)
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(Properties.Settings.Default.InformationLogsDestination, "Information logs"));
                string logName = "log" + DateTime.Now.ToString("yyMMdd") + ".txt";
                fs = new FileStream(string.Format(@"{0}\Information logs\{1}", Properties.Settings.Default.InformationLogsDestination, logName), FileMode.Append);
                w = new StreamWriter(fs, Encoding.UTF8);
            }


Each of my methods that I use this at the end. I kind of wanted a open and close thing. Because I don't know how to save the information when the web service changes it's URL.
C#
if (Properties.Settings.Default.Trace)
            {
                w.Flush();
                w.Close();
                fs.Close();
            }


But I keep getting the error where line 32 is pointing at the fs = new FileStream... in my constructor:
System.Web.Services.Protocols.SoapException was unhandled
Message=System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.IO.IOException: The process cannot access the file '\\9400D9\Users\sl9400\Documents\Information logs\log110712.txt' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at WebService1.Service1..ctor() in C:\Users\sl9400\documents\visual studio 2010\Projects\ProgramCommunicationBridge\WebService1\CnsService.asmx.cs:line 32
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
--- End of inner exception stack trace ---


if someone can help me, that would give me so much help!
Posted
Updated 12-Jul-11 12:28pm
v2

Is this happening on the very first instance that's created? Or only on all the rest of them?

See:

http://msdn.microsoft.com/en-us/library/47ek66wy.aspx[^]

Which states in part:

"requests to open the file for writing by this or another process will fail until the FileStream object has been closed, but read attempts will succeed"

It looks like you are creating the filestream in the constructor, but don't close it until you use other method.

The filestream is opened for write by the very first instance that gets created. Any subsequent instances will fail because they can't get write access.

Minimally you should move the creation of the Filestream out of your contstructor and only do it just before you write to it, then immediately close it after writing to it.

But even that won't work if you get more than one request from different clients at the same time.

The easiest thing to do would be to log the log entries into your database -- the database will handle the locking / concurrency issues for you.
 
Share this answer
 
Maybe you should try disposing the streams, not just close them:
w.Dispose();
fs.Dispose();
 
Share this answer
 
Comments
Member 8015046 12-Jul-11 19:09pm    
Thanks it worked! hahahha
JOAT-MON 12-Jul-11 19:11pm    
Perfect! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900