I had asked a question here: The process cannot access the file because it is being used by another process: Correct way to read\write to a file: heavily used application-Part II.
We have a heavily used .Net 3.5 application that reads "expensive to create" data and caches it. However we are getting a lot of errors around both reading the cache file and writing to the cache file. I have a single process, multiple threads and I want the application to synchronize access to a resource. I was advised to use a simple locking mechanism like lock or ReaderWriterLockSlim (see: http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx). This however seems to have made the problem much much worse in production.
EDIT
After the change was implemented, a lot of the cache files have a ">" tag in the end. Due to this the files are no longer xml files.
Can someone look at the code and advise what could I be doing wrong?
Code before change:
private XmlDocument ReadFromFile()
{
XmlDocument result=null;
string fileSystemPath=FileSystemPath();
try
{
result=new XmlDocument();
using (StreamReader streamReader = new StreamReader(fileSystemPath))
{
result.Load(streamReader);
}
}
catch (FileNotFoundException)
{
result=null;
}
return result;
}
private object thisObject= new object();
private void WriteToFile(string siteID, XmlDocument stuff)
{
string fileSystemPath=FileSystemPath();
lock(thisObject)
{
using (StreamWriter streamWriter = new StreamWriter(fileSystemPath))
{
stuff.Save(streamWriter);
}
}
}
Code after change:
private readonly ReaderWriterLockSlim readerWriterLockSlim = new ReaderWriterLockSlim();
private XmlDocument ReadFromFile()
{
XmlDocument result = null;
var fileSystemPath = FileSystemPath();
readerWriterLockSlim.EnterReadLock();
try
{
result = new XmlDocument();
using (var fileStream = new FileStream(fileSystemPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var streamReader = new StreamReader(fileStream))
{
result.Load(streamReader);
}
}
catch (FileNotFoundException)
{
result = null;
}
finally
{
readerWriterLockSlim.ExitReadLock();
}
return result;
}
private void WriteToFile()
{
var fileSystemPath = FileSystemPath();
readerWriterLockSlim.EnterWriteLock();
try
{
using (var fileStream = new FileStream(fileSystemPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
using (var streamWriter = new StreamWriter(fileStream))
{
stuff.Save(streamWriter);
}
}
finally
{
readerWriterLockSlim.ExitWriteLock();
}
}
This code with some little changes should work
private readonly ReaderWriterLockSlim readerWriterLockSlim = new ReaderWriterLockSlim();
private XmlDocument ReadFromFile()
{
XmlDocument result = null;
var fileSystemPath = FileSystemPath();
readerWriterLockSlim.EnterReadLock();
try
{
result = new XmlDocument();
using (var streamReader = new StreamReader(fileStream))
{
result.Load(streamReader);
}
}
catch (FileNotFoundException)
{
result = null;
}
finally
{
readerWriterLockSlim.ExitReadLock();
}
return result;
}
private void WriteToFile(XmlDocument stuff)
{
var fileSystemPath = FileSystemPath();
readerWriterLockSlim.EnterWriteLock();
try
{
using (var streamWriter = new StreamWriter(fileSystemPath))
{
stuff.Save(streamWriter);
}
}
finally
{
readerWriterLockSlim.ExitWriteLock();
}
}
Related
I have a web application that checks for the modification of a config.json file on each page load. It checks the modified date of the file and compares it against the last processing time that was recorded. If it differs, it proceeds to allow the reading of the file contents, processing those contents and updating config.json as well as writing to a separate file. I want to ensure that multiple simultaneous connections both reading and writing to these two files won't cause an issue.
var lastWriteTime = File.GetLastWriteTimeUtc(ConfigJsonPath);
if (CacheTime != lastWriteTime)
{
var config = new ConfigWriter().ReadData(ConfigJsonPath);
var model = JsonConvert.DeserializeObject<StyleModel>(config);
// This method writes to a another file
ProcessConfig(model, page);
var serialized = JsonConvert.SerializeObject(model, Formatting.Indented);
new ConfigWriter().WriteData(serialized, ConfigJsonPath);
CacheTime = File.GetLastWriteTimeUtc(ConfigJsonPath);
}
ConfigWriter Class
public class ConfigWriter
{
private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
public void WriteData(string data, string path)
{
Lock.EnterWriteLock();
try
{
using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
fs.SetLength(0);
var dataAsByteArray = new UTF8Encoding(true).GetBytes(data);
fs.Write(dataAsByteArray , 0, dataAsByteArray .Length);
}
}
finally
{
Lock.ExitWriteLock();
}
}
public string ReadData(string filePath)
{
Lock.EnterReadLock();
try
{
string config;
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (var r = new StreamReader(fs, Encoding.UTF8))
{
config = r.ReadToEnd();
}
}
return config;
}
finally
{
Lock.ExitReadLock();
}
}
}
So in the end there is one file being read, and two files being altered (write), and I'm using the same lock on the write.
Did I overdo it here? I started with a simple filestream with FileShare set to read/write, and then I got paranoid and second guessing myself. Is there a better way to implement this?
I'm trying to store my query result into XML in isolated storage application.
Here's my source in regards of the query, But the problem is I cannot cast the Productlist into a List or Iqueryable so I can pass the data to Save_Product() method. Thanks in advance for the help guys.
private void loadProductCombobox()
{
productDomainContext = new ProductDS();
EntityQuery<product> bb = from b in productDomainContext.GetProductsQuery() select b;
LoadOperation<product> res = productDomainContext.Load(bb, new Action<LoadOperation<product>>(loadProductComboboxcompleted), true);
}
private void loadProductComboboxcompleted(LoadOperation<product> obj)
{
selectProductComboBox.ItemsSource= productDomainContext.products;
****************Issue causing line*************
IEnumerable<product> productList = (IEnumerable<product>)productDomainContext.products;
List<product> productlist2 = (List<product>)productList;
Save_Product(productlist2);
*******************
}
public static void Save_Product(List<product> product)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = store.OpenFile("Product.XML", FileMode.OpenOrCreate, FileAccess.Write))
{
using (TextWriter writer = new StreamWriter(stream))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<product>));
serializer.Serialize(writer, product);
}
}
}
}
I have also tried to do this:
private void loadProductCombobox()
{
productDomainContext = new ProductDS();
EntityQuery<product> bb = from b in productDomainContext.GetProductsQuery() select b;
LoadOperation<product> res = productDomainContext.Load(bb, new Action<LoadOperation<product>>(loadProductComboboxcompleted), true);
}
private void loadProductComboboxcompleted(LoadOperation<product> obj)
{
selectProductComboBox.ItemsSource= productDomainContext.products;
Save_Product(productDomainContext.products);
}
public static void Save_Product(EntitySet<product> product)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = store.OpenFile("Product.XML", FileMode.OpenOrCreate, FileAccess.Write))
{
using (TextWriter writer = new StreamWriter(stream))
{
XmlSerializer serializer = new XmlSerializer(typeof(EntitySet<product>));
serializer.Serialize(writer, product);
}
}
}
}
It says it cannot be serialized becuase it has to be ienumerable
I cannot figure out why the serializer cannot work directly from entity, but I worked around it by making a list of the copy of model then I serialized it.
Hopefully this will help.
I have a Game application(WP8), where we are saving the scores of multiple attampts and showing it to user.
I have a Object with fields noOfStonesPicked and noOfFruitsPicked.
Here is my code:
MyTopic topicObj = new MyTopic ();
for (int i = 0; i <= 2; i++)
{
Test mt = new Test();
mt.noOfStonesPicked = 12;
mt.noOfFruitsPicked= 20;
topicObj.Stats.Add(mt);
}
WritetestTopicState(topicObj);
Now 3 attempts with each one having noOfStonesPicked -12 and noOfFruitsPicked - 20
Now i have saving this like :
public static void WritetestTopicState(MyTopic topic)
{
try
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamWriter sw = new StreamWriter(store.OpenFile("12.xml", FileMode.Append, FileAccess.Write)))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
serializer.Serialize(sw, topic);
serializer = null;
}
}
}
catch (Exception)
{
throw;
}
}
Now how can i retrive these values and display ?
EDIT
This is what i have tried:
public static MyTopic ReadMockTestTopicState()
{
MyTopic topic = null;
try
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
// Read application settings.
if (isoStore.FileExists("11.xml"))
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamReader SR = new StreamReader(store.OpenFile("12.xml", FileMode.Open, FileAccess.Read)))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
topic = (MyTopic)serializer.Deserialize(SR);
serializer = null;
}
}
}
else
{
// If setting does not exists return default setting.
topic = new MyTopic();
}
}
}
catch (Exception)
{
throw;
}
return topic;
}
XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
StreamReader reader = new StreamReader(path);
_myTopic = (MyTopic)serializer.Deserialize(reader);
reader.Close();
This should be enough for deserializing, If your MyTopic object is properly serializable, I mean if properties of the MyTopic object are properly attributed for xml serialization.
I have seen numerous similar questions and I have really tried all the solutions but none seems to work for me.
This is what I have now:
private readonly object _lock = new object();
List<DataModel> dataList = new List<DataModel>();
lock (_lock)
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
dataList = (List<DataModel>)serializer.Deserialize(stream);
}
}
catch (IsolatedStorageException e) { e.ToString(); }
}
}
The error occurs on the line using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)).
I deduced from the other solutions that I read that this error mainly occurs when I want to write to the file while others are reading it or execute that code block several times concurrently, I end up locking the file."
With the presence of the lock, FileAccess.ReadWrite and FileShare.ReadWrite statements, I'm pretty sure something else is throwing that exception.
My question is what could be throwing the exception (IsolatedStorageException) and how do I take care of it?
There is no InnerException on this one.
Edit: Upon Kookiz suggestion, I'm including this code lines
First, I create my .xml file like this:
public static void createDataXML()
{
List<DataModel> dataList = new List<DataModel>();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists("Data.xml"))
{ return; }
using (IsolatedStorageFileStream stream = myIsolatedStorage.CreateFile("Data.xml"))
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
serializer.Serialize(stream, dataList);
}
catch
{ }
}
}
}
Later, I populate with this code:
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, dataList);
}
}
}
I know this is a less conventional way of doing this but it IS a way. I was attempting to do something else and this was the result. Just add in the known type you need and this will work
[DataContractAttribute]
[KnownType (typeof(List<String>))]
public class SerializableObject
{
[DataMember]
public List<String> serFile { get; set; }
}
public static Object GetFile(String FileName)
{
try
{
if (!IsolatedStorageFile.GetUserStoreForApplication().FileExists(FileName))
{
throw new System.ArgumentException("File Doesn't Exist In Isoloated Storage");
}
}
catch { return null; }
Object ret = new Object();
try
{
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(#"\" + FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
SerializableObject serList = new SerializableObject();
DataContractSerializer dsc = new DataContractSerializer(serList.GetType());
ret = ((SerializableObject)dsc.ReadObject(fileStream)).serFile;
}
catch (Exception error) { throw new System.ArgumentException(error.Message); }
return ret;
}
The implied task here is that you need to serialize it within a SerializableObject instance. Let me know if you need that code also
Edit
As promised, the savefile function
public static void SaveFile(String FileName, List<String> File)
{
try
{
if (FileName.Length < 1)
{
throw new System.ArgumentException("File Name Must Not Be Empty");
}
if (IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace <= 0)
{
throw new System.ArgumentException("Isolated Storage Out of Memory - Please free up space.");
}
if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(FileName))
{
throw new System.ArgumentException("File Already Exists - Please choose a unique name.");
}
if (File == null)
{
throw new System.ArgumentException("Cannot Save Null Files");
}
}
catch (Exception e)
{
return;
}
try
{
SerializableObject so = new SerializableObject() { serFile = File };
DataContractSerializer dsc = new DataContractSerializer(so.GetType());
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writer;
writer = new StreamWriter(new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, file));
dsc.WriteObject(writer.BaseStream, so);
}
catch (Exception error) { throw new System.ArgumentException(error.Message); }
}
Enjoy serializing!
My dear, though this exception of Operation Not Permitted On IsolatedStorage is mainly due to ReadWrite by another thread, in your case it is because of ONE SIMPLE REASON
FileMode.Open throws exception when the filename DOES NOT EXIST
Try with FileMode.OpenOrCreate and it will work like a charm
List<DataModel> dataList = new List<DataModel>();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
dataList = (List<DataModel>)serializer.Deserialize(stream);
}
}
I need to read csv file twice. but after first reading:
using (var csvReader = new StreamReader(file.InputStream))
{
fileFullText += csvReader.ReadToEnd();
file.InputStream.Seek(0, SeekOrigin.Begin);
csvReader.Close();
}
using file in enother function:
public static List<string> ParceCsv(HttpPostedFileBase file)
{
//file.InputStream.Seek(0, SeekOrigin.Begin);
using (var csvReader = new StreamReader(file.InputStream))
{
// csvReader.DiscardBufferedData();
// csvReader.BaseStream.Seek(0, SeekOrigin.Begin);
string inputLine = "";
var values = new List<string>();
while ((inputLine = csvReader.ReadLine()) != null)
{
values.Add(inputLine.Trim().Replace(",", "").Replace(" ", ""));
}
csvReader.Close();
return values;
}
}
The file.Length is 0.
Can anybody help?
The reason is that SteramReader's Dispose() method also closes the underlying stream; In your case file.InputStream. The using statement calls Dispose() implicitly. Try to replace using with disposes of both your StreamReaded-s after you finished both read operations. As I remember some stream classes have a bool option to leave underlying stream open after dispose.
.NET 4.5 fixed this issue by introducing leaveOpen parameter in SteamReader constructor. See: MSDN
public StreamReader(
Stream stream,
Encoding encoding,
bool detectEncodingFromByteOrderMarks,
int bufferSize,
bool leaveOpen
)
One more thing. You do not need to close SteramReader yourself (the line with csvReader.Close();) when you wrap it in using statement, thus Dispose() and Close() are the same in case of StreamReader.
if your using HttpPostedFileBase you need to clone it first,
use the code this git here
or just add this as a class in your namespace:
public static class HttpPostedFileBaseExtensions
{
public static Byte[] ToByteArray(this HttpPostedFileBase value)
{
if (value == null)
return null;
var array = new Byte[value.ContentLength];
value.InputStream.Position = 0;
value.InputStream.Read(array, 0, value.ContentLength);
return array;
}
}
now you can read the HttpPostedFileBase like so:
private static void doSomeStuff(HttpPostedFileBase file)
{
try
{
using (var reader = new MemoryStream(file.ToByteArray()))
{
// do some stuff... say read it to xml
using (var xmlTextReader = new XmlTextReader(reader))
{
}
}
}
catch (Exception ex)
{
throw ex;
}
}
after using this you can still write in your main code:
file.SaveAs(path);
and it will save it to the file.