XML错误:对于无符号字节,值太大或太小

时间:2016-09-16 02:32:46

标签: json xml asp.net-mvc linq-to-xml

我对此完全是绿色的,它让我疯狂 - 它适用于某些数据集但不适用于其他数据集。

编辑:看起来它与记录数量有关 - 不知何故,当记录数量达到一定数量时 - 可能就像250一样 - 当它崩溃时就是这样。似乎涉及到自动递增ID主键,因为我可以看到当我在ID列进入400时添加甚至3条记录但是所有记录都被删除时它仍然产生相同的错误但是当我重置ID时为零 - 可以在XML正文中添加相同的3条记录并进行适当格式化。

映射类型:Int32 - > Byte System.Int32 - > System.Byte目标路径:FixtureDto.ExchangeType.ExchangeType.Id.Id源值:268

对于无符号字节,值太大或太小。

'ObjectContent`1'类型无法序列化内容类型'application / xml的响应主体;字符集= UTF-8' 。

enter image description here

<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>
Mapping types: Int32 -> Byte System.Int32 -> System.Byte Destination path: FixtureDto.ExchangeType.ExchangeType.Id.Id Source value: 268
</ExceptionMessage>
<ExceptionType>AutoMapper.AutoMapperMappingException</ExceptionType>
<StackTrace>
at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext() at WriteArrayOfFixtureDtoToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.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.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.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Value was either too large or too small for an unsigned byte.
</ExceptionMessage>
<ExceptionType>System.OverflowException</ExceptionType>
<StackTrace>
at System.Convert.ToByte(Int32 value) at System.Int32.System.IConvertible.ToByte(IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.ComponentModel.BaseNumberConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType) at AutoMapper.Mappers.TypeConverterMapper.<>c__DisplayClass1_0.<GetConverter>b__0() at AutoMapper.Mappers.TypeConverterMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
</StackTrace>
</InnerException>
</InnerException>
</Error>

1 个答案:

答案 0 :(得分:3)

解决!

我已将数据库类定义中的数据类型从byte更改为int,但忽略了在我的等效数据传输映射对象中镜像相同的更改。

这已经是一方面的荆棘,第三次试图找到它的底部,前一次尝试herehere

最后,通过艰苦的重复测试,我意识到这与向表格添加索引或者错误数据或重新设置ID自动增量无关,而是通过数据库中的记录数量在显示中受限制数据传输映射错误 - 即字节定义的ID列限制数量。

我已经将dto定义更改为int以匹配数据库,现在它可以根据应有的情况精确地扩展到超过255条记录。 enter image description here