We have an image loader class, which load customer image from file system by customer ID. Some how, sometimes (very few) the different customer ID has the same images which are NOT belong that customer. I think it is the singleton class cause the problem, but I cannot prove or reproduce the problem. How can I reproduce the problem??
[Serializable()]
class ImageLoader : Csla.CommandBase
{
....
private static object lockImageLoader = new object();
public static ImageLoader LoadImage(int customerID)
{
lock (lockImageLoader)
{
ImageLoader original = new ImageLoader(customerID);
// Return an instance of the image loader.
return DataPortal.Execute<ImageLoader>(original);
}
}
protected override void DataPortal_Execute()
{
// Load image from file system
System.IO.FileStream fs = null;
byte[] buffer = new byte[0];
try
{
fs = System.IO.File.Open(this._fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
fs.Close();
}
catch (Exception ex)
{
throw ;
}
this._blob = buffer;
}
Why are you using a lock on the client side? To prevent the client from making more than one concurrent call to the app server? You do realize that multiple clients will still call the app server concurrently, because a lock exists within the context of only that one client app instance?
I can only assume you aren't showing us some code that makes this a "singleton"? Perhaps some client-side caching or something?
I don't know the answer.
I do know that the code you posted here, assuming this is all the code, doesn't need the lock, because it isn't doing anything that would require a lock.
Just because you use a static method doesn't mean you have a singleton, or an issue requiring locking.
If you have a static field then you could have an issue, because then you'd have something in a shared memory space. But your code doesn't have a static field, so you don't appear to have any shared memory.
HI,I got ths same problem as yours.And I 've searched alot. ultimatly ,I solved that by the Image loading guide with Csla.CommandBase from Google.I post it and hope that it could be helpful for someone in trouble.
Copyright (c) Marimer LLC