3
我們將以下類作爲使用WCF在線上運行的ADO.NET提供程序的一部分。protobuf -net不知道如何序列化類型爲= object的datamember
[KnownType(typeof(AdoServiceCommandExecutionScalarResult))]
[DataContract(Namespace = Namespaces.SoafXmlNamespace)]
public class AdoServiceCommandExecutionResult
{
}
[DataContract(Namespace = Namespaces.SoafXmlNamespace)]
public class AdoServiceCommandExecutionScalarResult : AdoServiceCommandExecutionResult
{
[DataMember]
public object Result { get; set; }
public override string ToString()
{
return new XDocument(new XElement(GetType().Name, new XAttribute("Result", Result))).ToString();
}
}
我們正在嘗試使用protobuf網系列化但有拋出該類型的對象不能被解析異常。
異常消息:
No serializer defined for type: System.Object
堆棧跟蹤:
ProtoBuf.Meta.ValueMember.BuildSerializer()
ProtoBuf.Meta.ValueMember.get_Serializer()
ProtoBuf.Meta.MetaType.BuildSerializer()
ProtoBuf.Meta.MetaType.get_Serializer()
ProtoBuf.Meta.MetaType.ProtoBuf.Serializers.ISerializerProxy.get_Serializer()
ProtoBuf.Serializers.SubItemSerializer.ProtoBuf.Serializers.IProtoTypeSerializer.HasCallbacks(CallbackType callbackType)
ProtoBuf.Serializers.TypeSerializer.HasCallbacks(CallbackType callbackType)
ProtoBuf.Serializers.TypeSerializer.ProtoBuf.Serializers.IProtoSerializer.EmitWrite(CompilerContext ctx, Local valueFrom)
ProtoBuf.Compiler.CompilerContext.WriteNullCheckedTail(Type type, IProtoSerializer tail, Local valueFrom)
ProtoBuf.Compiler.CompilerContext.BuildSerializer(IProtoSerializer head, TypeModel model)
ProtoBuf.Serializers.CompiledSerializer..ctor(IProtoTypeSerializer head, TypeModel model)
ProtoBuf.Meta.MetaType.get_Serializer()
ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest)
ProtoBuf.Meta.TypeModel.TrySerializeAuxiliaryType(ProtoWriter writer, Type type, DataFormat format, Int32 tag, Object value, Boolean isInsideList)
ProtoBuf.Meta.TypeModel.TrySerializeAuxiliaryType(ProtoWriter writer, Type type, DataFormat format, Int32 tag, Object value, Boolean isInsideList)
ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value)
ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context)
ProtoBuf.ServiceModel.XmlProtoSerializer.WriteObjectContent(XmlDictionaryWriter writer, Object graph)
System.Runtime.Serialization.XmlObjectSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph)
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota)
System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(Message message)
System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReplyCore(Message message, TimeSpan timeout)
System.ServiceModel.Channels.HttpRequestContext.OnReply(Message message, TimeSpan timeout)
System.ServiceModel.Channels.RequestContextBase.Reply(Message message, TimeSpan timeout)
System.ServiceModel.Channels.RequestContextBase.Reply(Message message, TimeSpan timeout)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Reply(MessageRpc& rpc)
是否有可能解決這個問題?
編輯: 我們有一個自定義的例程,基本上(簡化爲簡潔)。
type.GetAttributes<KnownTypeAttribute>().Select(a => a.Type).Distinct().ForEach(t => AddKnownTypeHierarchy(t));
public static void AddKnownTypeHierarchy(Type type)
{
ProtoBuf.Meta.MetaType metaType = RuntimeTypeModel.Default.Add(type, true);
...
var fieldNumber = derivedType.MetadataToken;
metaType.AddSubType(fieldNumber, derivedType);
...
var memberType = member.MemberInfo.As<PropertyInfo>().IfNotNull(p => p.PropertyType) ?? member.As<FieldInfo>().IfNotNull(f => f.FieldType);
var field = metaType.AddField(member.Order, member.Name);
if (memberType == typeof (object) || memberType.EqualsGenericTypeFor(typeof (IEnumerable<>))) field.DynamicType = true;
...
}
我們添加邏輯,如果一個對象被遇到,它應該動態地處理。 這種做法導致了以下異常:
Dynamic type is not a contract-type: Int32
是否有可能增加對治療對象的動態,而不是支持?
請您報告*確切*異常信息? – 2013-03-06 07:47:05
編輯問題以包含異常消息和堆棧跟蹤 – 2013-03-06 15:55:55
是否可以將對象對象的支持添加爲動態對象? – 2013-03-06 21:02:58