ImageLoader : Csla.CommandBase cannot load image properly sometimes

ImageLoader : Csla.CommandBase cannot load image properly sometimes

Old forum URL: forums.lhotka.net/forums/t/6595.aspx


fudongma posted on Wednesday, March 11, 2009

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

 

 

RockfordLhotka replied on Wednesday, March 11, 2009

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?

fudongma replied on Wednesday, March 11, 2009

Thank you for your reply, Rocky.
We got image display issue now: one customer can see other customer's images. From the code analysis, this part makes me think there are some errors here, but I am not sure: because although we have singleton call (static method, lock is to prevent multi-thread for that client call), the customer ID is unique, which means different calls/clients have different instance for the client.
I can change the method: delete static, and remove lock. But does that help me solve the problem?? How can I prove that my problem will be gone using changed code?? Thank you.

RockfordLhotka replied on Thursday, March 12, 2009

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.

smoothing replied on Tuesday, May 28, 2013

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