I'm trying to serialize a type using the DataContractSerializer and am getting the exception below. This isn't for an SOA service, but I would still like to use the DataContractSerializer if possible. I am using .Net 3.5 SP1.
Type
'System.DelegateSerializationHolder+DelegateEntry'
with data contract name
'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System'
is not expected. Add any types not
known statically to the list of known
types - for example, by using the
KnownTypeAttribute attribute or by
adding them to the list of known types
passed to DataContractSerializer.
Can you post your class definition?
It seems like you are trying to serialize a class which has a field of type delegate, which I'm pretty sure will make the serializer choke on.
Did you decorate your class with the DataContract / DataMember attributes? In 3.5 SP1 there is a default behavior for the serializer that serializes everything public in a class by default if it is not marked with those attributes. Maybe you should explicitely mark each property that needs to be serialized with a DataMember attribute and leave out those that should not be.
Other than that, we would need to see your class definition for more help.
There are 3 possible approaches to avoid the error described in http://blogs.microsoft.co.il/blogs/oshvartz/archive/2009/10/10/passing-event-handlers-over-wcf.aspx
Related
I am using Catel Framework in a Desktop WPF app and when I try to save a model to a file I get the following error.
Type 'GeoChemicalFuncsCS.Core.Models.BoilingModel' with data contract name 'BoilingModel:http://schemas.datacontract.org/2004/07/GeoChemicalFuncsCS.Core.Models' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
I tried adding the KnownType and ServiceKnownType Tag to the model, but I get:
KnownType could not be found.
or
ServiceKnownType could not be found.
And I also tried WarmingUp the SerializationFactory in the ViewModel:
var typesToWarmup = new Type[] { typeof(BoilingModel) };
SerializationFactory.GetXmlSerializer().Warmup(typesToWarmup);
but nothing changed.
Does anyone have any suggestion? What can I do at this point?
Thanks
Saul Hidalgo.
It looks like you (or the Catel library) are somehow trying to use the WCF DataContractSerializer for simple serialization of a plain object to an XML file. That is not what the DataContractSerializer is designed for - it's basically designed for serializing data objects to SOAP to be sent over a WCF web service, and requires complex configuration.
For simple XML serialization, annotate the class / struct to be serialized with [Serializable] and use System.Xml.Serialization.XmlSerializer.
Example (not tested):
var boilingModelObj = new BoilingModel();
// ... fill object with data ...
var serializer = new XmlSerializer(typeof(BoilingModel));
using (var writer = new StreamWriter("boilingmodel.xml"))
{
serializer.Serialize(writer, boilingModelObj);
}
After some work I finally solved it.
Problem was originated because I had a collection which contained 2 types of objects (both serializable). I tried to generalize it creating a collection of "object", and when I needed to use it I casted it.
Well, serializer engine found the type "object", and when he tried to serialize, he found it really was a BoilingModel.
Solution was just doing an interface, and now both serializable types implement that interface.
I hope it helps someone else with the same problem.
Regards
Saul.
I have a custom class that only has one property of type string.
Attempting to serialise an object of this class as an attribute using XmlSerializer results in the exception:
XmlAttribute/XmlText cannot be used to encode complex types
Now I know I can fudge this my creating a pseudo string property in my containing type and serialising that instead, but is there any way at all to make my custom class support it intrinsically?
How do .Net classes do it (eg DateTime)?
In order to this you have to implement IXmlSerializable and fully control how your type is serialized / deserialized. Rather overwhelming effort for such an easy task, though. People mostly get stuck with this when it comes to Nullable types.
When serializing a class I get an error if the XmlIgnore attribute is commented. When I uncomment the XmlIgnore attribute it works fine. Can anyone tell me why a property can't be serializable?
Normally, it will tell you why very clearly in the exception - look in particular at the InnerException; however, to summarise:
For a custom type SomeType to be serializable via XmlSerializer (either as the root object or via a property, i.e. public SomeType MemberName {get;set;})
it must be public
it must have a public parameterless constructor
a few generic combinations may not be supported
it must be expected (exposing data as object is a no-go, for example; subtypes of SomeType must be advertised in advance, typically via [XmlInclude(...)])
it must be concrete (non-abstract), or have concrete implementations defined via [XmlInclude(...)]
Using [XmlIgnore] removes the type from consideration, so types that are not xml-friendly can be avoided. You can also try IXmlSerializable for those cases
We just started a new ASP.Net project that uses web services to serialize some CSLA business objects into JSON data for our client javascript/JQuery code. After reviewing the JSON data in the client browser(Firebug in Firefox) we notices that there are a significant number of properties from the business object that we do not need downloaded to the browser.
Is there a way to exclude properties (other than marking them private) from getting serialized by the JSON serializer? We are not calling the JSONSerializer directly, but instead just included a ScriptMethod declaration on the WebMethod.
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _
Public Function getQuestions()
UPDATE
We tried the suggestion of adding the attribute on the public property but received an error:
Error 25 Attribute 'NonSerializedAtrribute' cannot be
applied to 'Name' because the attribute is not valid
on the is declaration type.
Now if we add NonSerialized to the class then it works but not on the property. However, we do want some properties to be serialized.
Any ideas?
You should use ScriptIgnore attribute for all properties which should be not serialized.
If you decide to make more customization of data serialization, for example, replacing one properties name with another one or converting some properties in an array and so on you can write a small JavaScriptTypeResolver which do it.
ScriptIgnore should do the job for you as sugested by Oleg. Check out this link for a detailed sample
you could try to place a NonSerializedAttribute on the properties not sure if it works with the Json serializer...
Edit: if you are using .net 4.0 you could try to use the ISerializable interface...
I have some types which are generated by a web service reference. I want to serialize these objects using the DataContractJsonSerializer, so I need to add DataContract and DataMember attributes. Adding DataContract is no problem using partial classes. But the properties have no DataMember attributes, so I only get empty objects. Is there a way to get this to work in case one cannot modify the serialized types?
Unlike XmlSerializer, I don't think (from memory) that there is a ctor for passing in this extra metadata at runtime. Perhaps another viable option is to have a twin DTO class that is attributed, and shuffle the data into there? You can add a conversion method / operator (between the two) in the partial class. Not ideal maybe, but it'll work.