文件流错误.帮助! [英] Filestream error. Help!

查看:205
本文介绍了文件流错误.帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否是要问的地方.这可能是一个简单的错误.但是我有一个调用文件流编写器的Web服务.

我以我的构造函数开始.如果将跟踪设置为true,则它将实例变量fs和w更改为那些变量.

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.

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);
            }



最后使用此方法的每种方法.我有点想要打开和关闭的东西.因为当Web服务更改URL时,我不知道如何保存信息.



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.

if (Properties.Settings.Default.Trace)
            {
                w.Flush();
                w.Close();
                fs.Close();
            }



但我在第32行指向我的构造函数中的fs = new FileStream ...时仍然出现错误:
未处理System.Web.Services.Protocols.SoapException
Message = System.Web.Services.Protocols.SoapException:服务器无法处理请求. ---> System.IO.IOException:该进程无法访问文件''\\ 9400D9 \ Users \ sl9400 \ Documents \ Information logs \ log110712.txt",因为该文件正在被另一个进程使用. 在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)
在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔值useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔值bFromProxy)
在System.IO.FileStream..ctor中(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,String msgPath,布尔bFromProxy)
在System.IO.FileStream..ctor(字符串路径,FileMode模式)下
在C:\ Users \ sl9400 \ documents \ visual studio 2010 \ Projects \ ProgramCommunicationBridge \ WebService1 \ CnsService.asmx.cs中的WebService1.Service1..ctor()中:第32行
在System.RuntimeType.CreateInstanceImpl中(布尔publicOnly,布尔skipVisibilityChecks,布尔fillCache)
---内部异常堆栈跟踪的结尾---


如果有人可以帮助我,那会给我很大帮助!



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!

推荐答案

这是在创建的第一个实例上发生的吗?还是只剩下全部?

参见:

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

部分说明:

关闭文件流对象之前,打开此文件或其他进程打开文件的请求将失败,但是读取尝试将成功"

看起来您正在构造函数中创建文件流,但是除非使用其他方法,否则不要将其关闭.

由创建的第一个实例打开文件流以进行写入.任何后续实例将失败,因为它们无法获得写访问权限.

至少应将文件流的创建移出构造器,并仅在写入之前执行,然后在写入后立即将其关闭.

但是,如果您同时收到来自不同客户的多个请求,那么即使这样也行不通.

最简单的方法是将日志条目记录到数据库中-数据库将为您处理锁定/并发问题.
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.


也许您应该尝试处置流,而不仅仅是关闭它们:
Maybe you should try disposing the streams, not just close them:
w.Dispose();
fs.Dispose();


这篇关于文件流错误.帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆