U4-2397 - IContent Properties added with incorrect Value when read from Cache

Created by Michael Falk 19 Jun 2013, 08:53:30 Updated by Michael Falk 12 Aug 2013, 14:30:00

Is duplicated by: U4-2249

Relates to: U4-2523

Hi I am using 6.1.1

I have the problem described here: http://our.umbraco.org/forum/developers/api-questions/41972-Weird-InvalidCastExceptions-when-using-ContentService

Basically the flow is:

  1. Clear cache (restart web server)
  2. Read data with content service, everything is fine
  3. Update a date field also with content service
  4. Goto Backend and clear the date field again, save and publish
  5. Now read the exact same way as in pt. 2, now the Content is returned from cache and all the properties Value properties is now texts, even though the type is indicated correctly (DataTypeDatabaseType is correct)

I will be glad to help to solve this problem, however i cannot find where the problem happens

br Michael

Comments

Michael Falk 29 Jun 2013, 09:40:37

Anybody?

Maybe just a hint on where to look. I assume it is the publish in the backend that messes this up?


Morten Christensen 01 Jul 2013, 07:46:01

I read the forum post and it sounds a bit weird to me. I'll have to investigate to verify the issue, but would be very surprised if caching was the issue as its the same object that is cached as is saved to the database. Maybe its when the object is fetched from the database once the cache has expired. But either way I'll have a look.


Michael Falk 01 Jul 2013, 07:47:50

Super, please let me know if i can provide you with anything to help tracking this down


Morten Christensen 01 Jul 2013, 11:14:38

Michael, I just did a test where I save a content item with a Number field and set the value to 42 using content.SetValue("number", 42); and I save it, retrieve it from cache, clear cache and retrieve it again and I don't have any problems doing content.GetValue("number"); on any of these content items. Am I missing something? Which DataType/PropertyType are you using for this bit: rs.GetValue("questionnaire") ? You wrote that it fails from time to time, and I'm assuming this is because its a string or null - based on what you wrote later in the thread, but I can't reproduce it. Maybe its because I'm using a different type.


Michael Falk 01 Jul 2013, 11:38:11

The questionnaire is a content picker. I also have booleans and datetime. the exact steps and code are:

Call my web service which does: var contentService = new Umbraco.Core.Services.ContentService(); var replies = contentService.GetContentOfContentType(1065).Where(t => (t.GetValue("importedInBackend") == null || string.IsNullOrEmpty(t.GetValue("importedInBackend").ToString())) && t.GetValue("submittedDate") != null && !string.IsNullOrEmpty(t.GetValue("submittedDate").ToString())); try { foreach (var rs in replies) { QuestionnaireReply r = new QuestionnaireReply();

                    var quest = contentService.GetById(rs.GetValue<int>("questionnaire"));

...stuff rs.SetValue("importedInBackend", DateTime.Now); contentService.SaveAndPublish(rs); } } catch (NullReferenceException) //Happens when there is nothing to get

then i go into the backend and on the same Item press clear in the ImportedInBackend property (Which offcource is a date

then i call the webservice again and now it is returning all properties as string on rs, and throws invalid cast exceptions


Morten Christensen 01 Jul 2013, 11:55:48

Is it the standard default/built-in content picker you are using? I tried adding that as well, and still not seeing any issues.

A side note: We always recommend getting the ContentService through the ApplicationContext (which resides in the Umbraco.Core namespace) as described here http://our.umbraco.org/documentation/Reference/Management-v6/Services/ContentService

ApplicationContext.Current.Services.ContentService


Michael Falk 01 Jul 2013, 12:08:46

Yes it is the standard content picker, however my date fields are also returned as string. I switched to ApplicationContext, but that did not change anything.

I can do a screen share if you would like to?


Michael Falk 22 Jul 2013, 14:54:49

I think i found the problem. It is the save method in BaseTreePickerEditor.cs, it adds the Id as string, but should be int. Changed code below:

public void Save() { if (ItemIdValue.Value.Trim() != "") _data.Value = int.Parse(ItemIdValue.Value.Trim()); else _data.Value = null; }

Please let me know if you agree?

br Michael


Morten Christensen 12 Aug 2013, 14:19:20

I can see how the Save method in the Content/Media/Member Picker would change the type from Int (as it supposed to be) to String. As soon as the cache is invalidated the value would be read from the database, and since it is stored correctly in the database there wouldn't be any issues. But as soon as the cache is updated with a value that has been set through the data type (would usually only occur when clicking Save/Save and Publish in Backoffice) the type would be changed to String again.

I have updated the Save method as mentioned above and updated the GetValue method on Content to be a bit more robust.


Michael Falk 12 Aug 2013, 14:30:00

Great news, thanks for the help


Priority: Normal

Type: Bug

State: Fixed

Assignee: Morten Christensen

Difficulty: Normal

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions: 6.1.1

Due in version: 6.1.4

Sprint:

Story Points:

Cycle: