This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, i have Member and MemberProfile models, and MemberProfile belongs to Member. I need to serialize object with values of all properties on it. When i try to serialize Member object, i get an error: A circular reference was detected while serializing an object of type Member.
FileStream file = new FileStream(file.xml, FileMode.Create); So to serialize this object i needed on MemberProfile class to hide Member property (which i don't need here) and instead of public access i give it internal internal EntityCollection<Member> Members The problem is when i change access type to internal end after that use LS Model Designer to update schema from Source my changes get overwritten. It would be great if we can explicitly set in designer Access type of properties, or some similar solution which will hide back references so we can serialize object.
Regards, Haris |
|
|
Similar situation @ http://www.west-wind.com/WebLog/posts/147218.aspx |
|
|
Hi Haris, That's a good idea and we'll look into it. We are also planning to allow you to put custom attributes on properties / associations via the designer; this would also address your issue because you could then put [XmlIgnore] on the Members property. I'll try to look at getting at least one of these features into the designer in the next couple of days. In the meantime, one option you could look at is injecting XmlIgnore attributes at runtime via the XmlAttributeOverrides overload of the XmlSerializer constructor. This is actually slightly better in the general case than static XmlIgnore attributes or internal members because which direction of the association you want to hide may depend on where the serialiser starts (e.g. does it start from the Member or the MemberProfile?); the downside is that it can be a bit laborious to set up from what I remember. |
|
|
Great, i'll wait for fix and in the mean time i'll try to find some workaround. |
|
|
Here is a quick (but naive) helper class that may help you with a workaround. The attached class creates an XmlSerializer that ignores circular references at the type level -- e.g. if Member has a MemberProfiles collection, and MemberProfile has a Member property, then it will XmlIgnore the MemberProfile.Member property on the basis that it has already seen an association between Member and MemberProfile in the other direction. Be warned that this class will be fooled if there are multiple associations between the same two entity types, e.g. if Contribution is associated to Member by both the Contributor and ApprovedBy associations. (If this is an issue then you could probably address it by adding code to look for the ReverseAssociationAttribute.) It will also not catch indirect circular references e.g. A has a B, B has a C and C has some As -- it is intended *only* to consider the LightSpeed EntityHolder/EntityCollection direct circular association. Usage is as follows: XmlSerializer ser = CircularReferenceIgnorer.CreateSerializer(typeof(Member)); Please consider this to be a proof of concept -- it is not very smart and has not had much testing, and is definitely not supported! But it may be enough for your scenario and if not it may help you to develop your own workaround. |
|
|
Thnx Ivan for your quick support (especially because our deadline is very near :)), i will test it and write back results of it.
|
|
|
Hi Haris, We now have experimental support for putting custom CLR attributes on properties through the designer, beginning in nightly builds dated 6 Dec 2008 and above (available from the store from about 1430 GMT) The user interface is a bit messy at the moment but it should be enough to get by -- hopefully we will have time to improve it in a future release. You can use this to apply the XmlIgnore attribute to properties that you want the XML serialiser to ignore, including the properties generated for associations. In your case, for example, you want the serialiser to ignore the MemberProfile.Members collection. So you would select the association between Member and MemberProfile, find "Collection Custom Attributes" in the properties box and click on the ellipsis button (you may need to click into the text box for this to appear). A dialog box appears: this will be empty because there are no custom attributes on the collection yet. Click Add New. In the box that appears, enter System.Xml.Serialization.XmlIgnore. (Note that you must provide the full type name including the namespace, because the generated files don't "using" anything except System.) XmlIgnoreAttribute doesn't have any parameters so don't worry about those. Click OK and save the model. You should now find that the XmlIgnore attribute is applied to the generated collection property (the Members property). As always, please let us know if you run into any bugs or problems. Thanks! |
|
|
Thanks Ivan, i will test it today and write back with results :) |
|
|
Hi Ivan, i haven't find any bug for now, everything works just great. Now i can serialize my objects as i wanted. Regards, Hars
|
|