Based on this article I was able to get the FullName to work rather easily.
I have the following class that has child classes as well:
[DataContract]
[Serializable]
public class SettingSection
{
public SettingSection()
{
this.UserSettings = new List<UserSettingPair>();
} // SettingSection - Constructor
public SettingSection(List<UserSettingPair> UserSettings)
{
this.UserSettings = UserSettings;
} // SettingSection - Constructor
[DataMember(Name = "sectionName")]
public string SectionName { get; set; }
[DataMember(Name = "userSettings")]
public List<UserSettingPair> UserSettings { get; set; }
} // SettingSection - Class
[DataContract]
[Serializable]
public class UserSettingPair
{
[DataMember(Name = "key")]
public string Key { get; set; }
[DataMember(Name = "value")]
public string Value { get; set; }
} // UserSettingPair - Class
I then have a way to serialize this into JSon with the following code:
public static string Serialize<T>(object input)
{
string Result = "";
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
ser.WriteObject(ms, input);
Result = Encoding.Default.GetString(ms.ToArray());
}
return Result;
}
When I do what is in the above article, the following works:
UserProfileContract.CurrentUser.FullName = "Testing";
When I try it with my List/Complex object (now a json formatting string)…
base["sectionSettings"] = (Utilities.Serialize<List<SettingSection>>(Settings)).ToString();
Save();
I get the following error (note above I even double forced it to a string with the .ToString() but no luck:
The settings property 'sectionSettings' is of a non-compatible type.
I am clearly doing something wrong, I have to assume I am not the first out there who wants to save json data in the ASP.Net default Profile provider. Any help would be greatly appreciated.
After doing some tests where I put the Utilities.Serialize… code into the working FullName, that worked. Then I renamed FullName to FullNameXX and it failed. After a few more tests I came to the conclusion that all of the properties to be saved in the Profile need to be strings (or Binary I assume but I am not using the PropertyValuesBinary data field in SQL.) So when I had a complex field, like my List then I had a property for the programmers that gets and sets a List as well as a string version of the same property that gets stored. So I now have a SectionSettings and a SectionSettingsValue property. Another adjustment needed was to make it only a DataContract and NOT Serializable or you will get extra values like k__backingField which IMHO looks bad.
The following should be the complete code…
Here is my DataContract:
Here is my static Utilities class:
Here is an example of how to Save data in multiple sections as well as the FullName property:
Here is an Extension Method I created to make extraction of SectionSetting values easier:
And lastly, an example of the above Extension method:
Here is the string version of the values I put in:
In the above string k = … example, note that it returns null because the data is “Primart Thing” and not “Primary Thing”.
Hope this helps someone out there.