Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

XML DataContract Formatter can fail to write when instance type != declaredType #1081

Closed
yishaigalatzer opened this issue Aug 29, 2014 · 5 comments
Assignees
Milestone

Comments

@yishaigalatzer
Copy link
Contributor

The formatter only looks at the declared type to make a decision about if a type is writeable, but can fail later at writing if the instance is invalid.

@yishaigalatzer
Copy link
Contributor Author

Related to #1073

@sornaks
Copy link

sornaks commented Sep 19, 2014

This doesnt work when DeclaredType is a base type and the actual RuntimeType is derived from the previous type. In WebAPI we return a 500 and put the exception in the response body for this scenario.
In our new world, the formatter just throws and does not send a 500 as response. @yishaigalatzer - Should we handle this scenario? Should we catch and set the response status to 500 and body with the exception specifically for this formatter?

@sornaks
Copy link

sornaks commented Sep 19, 2014

FYI: Response in case of WebAPI:

An error has occurred.The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.System.InvalidOperationExceptionAn error has occurred.Type 'WebApiXmlSerializer.Controllers.DerivedApple' with data contract name 'DerivedApple:http://schemas.datacontract.org/2004/07/WebApiXmlSerializer.Controllers' 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.System.Runtime.Serialization.SerializationException at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()

@rynowak
Copy link
Member

rynowak commented Sep 19, 2014

This is because you need to add [KnownType(typeof(DerivedApple))] to the base type. Try that and see if it still fails.

http://stackoverflow.com/questions/8555089/datacontract-and-inheritance

@sornaks
Copy link

sornaks commented Sep 19, 2014

Checked in - 4c951cc

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants