T
Tim_Mac
hi,
i'm not sure if i have chosen the best approach, but it seemed quite
good to me.
i have a collection class, containing business objects. the collection
class is static and remains in-memory all the time the app is running.
it is persisted to an Xml file when necessary. i keep a static
filestream on the xml file. the advantage of this is that i can lock
the filestream object to prevent concurrent reading/writing.
the code works great, but very occassionally, windows spits out a
WinIOError and the Save() method fails while calling SetLength(0) on
the filestream. the error message is:
"The requested operation cannot be performed on a file with a
user-mapped section open"
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
+2015079
System.IO.FileStream.SetLengthCore(Int64 value) +112
System.IO.FileStream.SetLength(Int64 value) +151
i thought by using "lock" on the filestream that the file i/o would be
safe. but i must be missing something? i searched around google for a
while but didn't find anything to explain why this is happening.
here is the code. it is very simple, with a private 'pages' variable,
and a corresponding public 'Pages' property, which handles
initialisation for first-time access.
public static class PageManager
{
private static PageCollection pages;
private static FileStream fs = new
FileStream(HttpContext.Current.Server.MapPath("~/App_Data/Pages.xml"),
FileMode.OpenOrCreate, FileAccess.ReadWrite);
public static PageCollection Pages
{
get
{
if (pages == null)
{
lock (fs)
{
// read in all the pages into memory
fs.Seek(0, SeekOrigin.Begin);
XmlSerializer xs = new
XmlSerializer(typeof(PageCollection));
pages = xs.Deserialize(fs) as PageCollection;
}
}
return pages;
}
}
public static void Save()
{
lock (fs)
{
fs.SetLength(0); // wipe the file contents
fs.Seek(0, SeekOrigin.Begin);
XmlSerializer xs = new
XmlSerializer(typeof(PageCollection));
xs.Serialize(fs, pages);
}
}
}
thanks for any tips!
tim
i'm not sure if i have chosen the best approach, but it seemed quite
good to me.
i have a collection class, containing business objects. the collection
class is static and remains in-memory all the time the app is running.
it is persisted to an Xml file when necessary. i keep a static
filestream on the xml file. the advantage of this is that i can lock
the filestream object to prevent concurrent reading/writing.
the code works great, but very occassionally, windows spits out a
WinIOError and the Save() method fails while calling SetLength(0) on
the filestream. the error message is:
"The requested operation cannot be performed on a file with a
user-mapped section open"
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
+2015079
System.IO.FileStream.SetLengthCore(Int64 value) +112
System.IO.FileStream.SetLength(Int64 value) +151
i thought by using "lock" on the filestream that the file i/o would be
safe. but i must be missing something? i searched around google for a
while but didn't find anything to explain why this is happening.
here is the code. it is very simple, with a private 'pages' variable,
and a corresponding public 'Pages' property, which handles
initialisation for first-time access.
public static class PageManager
{
private static PageCollection pages;
private static FileStream fs = new
FileStream(HttpContext.Current.Server.MapPath("~/App_Data/Pages.xml"),
FileMode.OpenOrCreate, FileAccess.ReadWrite);
public static PageCollection Pages
{
get
{
if (pages == null)
{
lock (fs)
{
// read in all the pages into memory
fs.Seek(0, SeekOrigin.Begin);
XmlSerializer xs = new
XmlSerializer(typeof(PageCollection));
pages = xs.Deserialize(fs) as PageCollection;
}
}
return pages;
}
}
public static void Save()
{
lock (fs)
{
fs.SetLength(0); // wipe the file contents
fs.Seek(0, SeekOrigin.Begin);
XmlSerializer xs = new
XmlSerializer(typeof(PageCollection));
xs.Serialize(fs, pages);
}
}
}
thanks for any tips!
tim