aspnet/AspNetWebStack

System.Runtime.Serialization.SerializationException at WriteJsonNetResultToXml

iqb-dawn opened this issue · 10 comments

We have Umbraco 8 CMS hosted in Azure as App Service. We see this exception,

System.Runtime.Serialization.SerializationException:
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at WriteJsonNetResultToXml (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.Serialization.DataContractSerializer.WriteObject (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream (System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync (System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Web.Http.WebHost.HttpControllerHandler+<WriteBufferedResponseContentAsync>d__22.MoveNext (System.Web.Http.WebHost, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)

I searched WriteJsonNetResultToXml in Google and Github but could not found this method.

What this exception means?

<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>

Azure settings are,

image

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project (ideally a GitHub repo) that illustrates the problem.

Its rarely happen in production so I can't provide repo. I got advice from David to file the issue here.

If you read the question where I asked I searched WriteJsonNetResultToXml in Google and Github but could not found this method.

Yea this stack trace might be partially bogus, I can't find WriteJsonNetResultToXml either.

Who owns System.Runtime.Serialization these days @davidfowl❔The stack trace (corrupt or not) may indicate a problem fairly far below our XmlMediaTypeFormatter.WriteToStream(...) method.

What really surprises me is the lack of a message for the SerializationException. Do you have more context e.g. nearby logging @iqb-dawn

@dougbu I got the same error again couple of days again with this message,

Date | 6/7/2022, 12:31:31 PM
Exception type | System.Runtime.Serialization.SerializationException |  
Failed method | WriteJsonNetResultToXml
Message | Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfKeyValueOfstringstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer 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 the serializer.
Problem Id | System.Runtime.Serialization.SerializationException at WriteJsonNetResultToXml |  
Assembly | System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |  
SDK version | web:2.20.0-103
Operation name | GET Icon/GetIcons
Call Stack | System.Runtime.Serialization.SerializationException:
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at WriteJsonNetResultToXml (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.ClassDataContract.WriteXmlValue (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.Serialization.DataContractSerializer.WriteObject (System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream (System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync (System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Web.Http.WebHost.HttpControllerHandler+d__22.MoveNext (System.Web.Http.WebHost, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)

It is now reproduceable when we hit https://myUmbracoSite/umbraco/backoffice/UmbracoApi/Icon/GetIcons, it will return the above error. So it is related to XML serialization. The same URL is working with application/json with no error.

Here is source line https://github.com/umbraco/Umbraco-CMS/blob/release-8.12.2/src/Umbraco.Web/Editors/IconController.cs#L52

What really surprises me is the lack of a message for the SerializationException. Do you have more context e.g. nearby logging

This is part of the PII stripping that app service does.

@iqb-dawn Can you reproduce this locally?

Yea, I have Umbraco CMS setup in my local machine and dev environment. When I hit this API https://myumbracosite/umbraco/backoffice/UmbracoApi/Icon/GetIcons in browser (which have Accept application/xml) then I am getting this error.

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfKeyValueOfstringstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer 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 the serializer.</ExceptionMessage>
<ExceptionType>System.Runtime.Serialization.SerializationException</ExceptionType>
<StackTrace> at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteJsonNetResultToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract ) at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) 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.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__22.MoveNext()</StackTrace>
</InnerException>
</Error>

Screen shot added

image

This error message looks more reasonable and I don't see the WriteJsonNetResultToXml text locally. What are you trying to serialize? This doesn't look like an issue specific to the framework.

It's MVC framework doing the XML serialization (when Umbraco CMS returning an object). The Umbraco API URL returns JsonResult https://github.com/umbraco/Umbraco-CMS/blob/release-8.12.2/src/Umbraco.Web/Editors/IconController.cs#L52.

As I said before its working when Accept: application/json used and I am not sure why Umbraco CMS is calling this API with Accept: application/xml.

Might be XML serializer is unable to serialize object meant only for JSON.

Thanks guys.