locked
OutOfMemoryExceptions - can I clear entity data without Lightswitch thinking it's changed? RRS feed

  • Question

  • I have a Silverlight client app that handles data for lectures.

    In order to allow the user to upload audio files for the lectures, the Lecture entity has a AudioData property (which is of type Binary in the Lightswitch designer, and varbinary(max) in the external database). The user can click a button on the lecture details, choose an audio file, and the data from this is placed in the Data property.

    When the entity is saved, the Lecture_Saving event is caught, and the audio data written to disk. The Data property is set to null, so the audio data never actually hits the database.

    This works fine, up to a point. At seemingly random intervals, I'll get an OutOfMemoryException and the app will crash.

    My suspicion is that when the data is saved, the copy of the entity that is on the client still has the audio data, so after a couple of files have been uploaded, I get the OutOfMemoryException. This seems to make sense, as if I refresh the lecture list after each upload, I don't seem to get the exception.

    So, what I would like to do is clear the Data property for the lecture after the entity has been saved, but not have Lightswitch think the entity has changed. Can I do this? If not, is there any other way to solve this, other than reloading the lecture list after each save (not practical due to the delay).

    Thanks


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Sunday, April 27, 2014 4:56 PM

Answers

  • Hi Mr. Yossu,

    I'm not sure why you would get this out of memory exception in a seemingly random fashion. It would make more sense if it only happens on large files over a certain size, where we could put it down to some sort of entity size threshold in LS, but that seems not to be the case.

    I assume this is a client side out of memory exception? If server side, you could perhaps also check the code used for writing out the file from the entity property to disk to make sure that is not the culprit.

    I wonder whether a totally different approach altogether is not the better solution here? How about adding an HTTP handler to the server project for receiving the files instead? You could use the lecture PK as a file name or even generate a GUID on the client side. We use this approach in a Silverlight client and it has been running in production for 2+ years without any problems.

    Here is a good article from Michael covering this concept:

    Saving Files To File System With LightSwitch (Uploading Files)

    Hope this helps.


    Regards, Xander. My Blog


    • Edited by novascape Sunday, April 27, 2014 9:08 PM
    • Marked as answer by Angie Xu Monday, May 5, 2014 2:49 AM
    Sunday, April 27, 2014 9:07 PM

All replies

  • Hmm, not convinced this is right. I just added some code to go through the lectures collection looking for entities whose Data was not null or of zero length, and it didn't find any, no matter how many times I uploaded files.

    Does the entity get pulled back from the server after being saved? If so, then my suspicion would be completely wrong.

    I'm stumped. Anyone any idea what could be causing the exception?


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Sunday, April 27, 2014 5:10 PM
  • Hi Mr. Yossu,

    I'm not sure why you would get this out of memory exception in a seemingly random fashion. It would make more sense if it only happens on large files over a certain size, where we could put it down to some sort of entity size threshold in LS, but that seems not to be the case.

    I assume this is a client side out of memory exception? If server side, you could perhaps also check the code used for writing out the file from the entity property to disk to make sure that is not the culprit.

    I wonder whether a totally different approach altogether is not the better solution here? How about adding an HTTP handler to the server project for receiving the files instead? You could use the lecture PK as a file name or even generate a GUID on the client side. We use this approach in a Silverlight client and it has been running in production for 2+ years without any problems.

    Here is a good article from Michael covering this concept:

    Saving Files To File System With LightSwitch (Uploading Files)

    Hope this helps.


    Regards, Xander. My Blog


    • Edited by novascape Sunday, April 27, 2014 9:08 PM
    • Marked as answer by Angie Xu Monday, May 5, 2014 2:49 AM
    Sunday, April 27, 2014 9:07 PM
  • Hi Xander, thanks for the reply.

    Unfortunately, I have no idea where the exception occurs. The exact message is...

    An unhandled exception of type 'System.OutOfMemoryException' occurred in Unknown Module.

    ...and the call stack is empty.

    As for the way I'm doing it, I did look at using a separate RIA service, but having done it this way before, it's much easier. I don't know why it's giving me such problems this time.

    Thanks again. Any further thoughts welcome.


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Monday, April 28, 2014 6:16 PM
  • I read answer of raphel  ... my fired sossy don't angry

    I have 

    try


                {

                    cn.Open();

                    

    string  DBQRY = "INSERT INTO TESTPHOTO(Photo) VALUES (:BLOBData1)";

                   

    OracleCommandmyconn = newOracleCommand(DBQRY, cn);

                   

    FileStreamfsBLOBFile = newFileStream(@imagename, FileMode.Open, FileAccess.Read);

                   

    Byte[] bytBLOBData = newByte[fsBLOBFile.Length];

                    fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);

                    fsBLOBFile.Close();

                   

    OracleParameterprm = newOracleParameter(":BLOBData1", OracleDbType.Blob, bytBLOBData.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, bytBLOBData);

                    myconn.Parameters.Add(prm);

                    myconn.ExecuteNonQuery();

                   

    MessageBox.Show("ONE DATA INSERTED SUCCESSFULLY");

                }

               

    catch


                {

    it is working correctly

    I I can create like this dynamically all all my pictrebox in my form like (:BLOBdata1,:Blobdata2,..) accrding to no pictureboxes in my form

    regards

    hotnthought

    Monday, April 28, 2014 6:24 PM
  • Mr. Yossu, just to be clear, a RIA service and an HTTP handler are obviously totally different things, I was not suggesting a RIA service in this case as the HTTP handler would in my opinion be better for file uploading.

    Regards, Xander. My Blog

    Monday, April 28, 2014 9:36 PM
  • Ah, my mistake, read too quickly! I know the difference between RIA and HTTP :)

    Might be worth trying it that way. Just a shame as it was so simple the other way.

    Thanks for the advice (as always!)


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Tuesday, April 29, 2014 2:41 PM