【发布时间】:2011-12-24 16:59:14
【问题描述】:
我有一个使用 webHttpBinding 端点的 C# WCF 服务,它将接收和返回 JSON 格式的数据。发送/接收的数据需要使用多态类型,以便不同类型的数据可以在同一个“数据包”中交换。我有以下数据模型:
[DataContract]
public class DataPacket
{
[DataMember]
public List<DataEvent> DataEvents { get; set; }
}
[DataContract]
[KnownType(typeof(IntEvent))]
[KnownType(typeof(BoolEvent))]
public class DataEvent
{
[DataMember]
public ulong Id { get; set; }
[DataMember]
public DateTime Timestamp { get; set; }
public override string ToString()
{
return string.Format("DataEvent: {0}, {1}", Id, Timestamp);
}
}
[DataContract]
public class IntEvent : DataEvent
{
[DataMember]
public int Value { get; set; }
public override string ToString()
{
return string.Format("IntEvent: {0}, {1}, {2}", Id, Timestamp, Value);
}
}
[DataContract]
public class BoolEvent : DataEvent
{
[DataMember]
public bool Value { get; set; }
public override string ToString()
{
return string.Format("BoolEvent: {0}, {1}, {2}", Id, Timestamp, Value);
}
}
我的服务将在单个数据包中发送/接收子类型事件(IntEvent、BoolEvent 等),如下所示:
[ServiceContract]
public interface IDataService
{
[OperationContract]
[WebGet(UriTemplate = "GetExampleDataEvents")]
DataPacket GetExampleDataEvents();
[OperationContract]
[WebInvoke(UriTemplate = "SubmitDataEvents", RequestFormat = WebMessageFormat.Json)]
void SubmitDataEvents(DataPacket dataPacket);
}
public class DataService : IDataService
{
public DataPacket GetExampleDataEvents()
{
return new DataPacket {
DataEvents = new List<DataEvent>
{
new IntEvent { Id = 12345, Timestamp = DateTime.Now, Value = 5 },
new BoolEvent { Id = 45678, Timestamp = DateTime.Now, Value = true }
}
};
}
public void SubmitDataEvents(DataPacket dataPacket)
{
int i = dataPacket.DataEvents.Count; //dataPacket contains 2 events, but both are type DataEvent instead of IntEvent and BoolEvent
IntEvent intEvent = dataPacket.DataEvents[0] as IntEvent;
Console.WriteLine(intEvent.Value); //null pointer as intEvent is null since the cast failed
}
}
当我将数据包提交给SubmitDataEvents 方法时,我得到DataEvent 类型并尝试将它们转换回它们的基本类型(仅用于测试目的)导致InvalidCastException。我的数据包是:
POST http://localhost:4965/DataService.svc/SubmitDataEvents HTTP/1.1
User-Agent: Fiddler
Host: localhost:4965
Content-Type: text/json
Content-Length: 340
{
"DataEvents": [{
"__type": "IntEvent:#WcfTest.Data",
"Id": 12345,
"Timestamp": "\/Date(1324905383689+0000)\/",
"Value": 5
}, {
"__type": "BoolEvent:#WcfTest.Data",
"Id": 45678,
"Timestamp": "\/Date(1324905383689+0000)\/",
"Value": true
}]
}
为这篇长文道歉,但我能做些什么来保留每个对象的基本类型吗?我认为将类型提示添加到 JSON 并将 KnownType 属性添加到 DataEvent 可以让我保留类型 - 但它似乎不起作用。
编辑:如果我以 XML 格式将请求发送到SubmitDataEvents(使用Content-Type: text/xml 而不是text/json),那么List<DataEvent> DataEvents 确实包含子类型而不是超级-类型。一旦我将请求设置为text/json 并发送上述数据包,我就只能得到超类型,我不能将它们转换为子类型。我的 XML 请求正文是:
<ArrayOfDataEvent xmlns="http://schemas.datacontract.org/2004/07/WcfTest.Data">
<DataEvent i:type="IntEvent" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Id>12345</Id>
<Timestamp>1999-05-31T11:20:00</Timestamp>
<Value>5</Value>
</DataEvent>
<DataEvent i:type="BoolEvent" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Id>56789</Id>
<Timestamp>1999-05-31T11:20:00</Timestamp>
<Value>true</Value>
</DataEvent>
</ArrayOfDataEvent>
编辑 2:更新了以下 Pavel 的 cmets 之后的服务描述。在 Fiddler2 中发送 JSON 数据包时,这仍然不起作用。我只是得到一个包含DataEvent 而不是IntEvent 和BoolEvent 的List。
编辑 3:正如 Pavel 所建议的,这是来自 System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.ToString() 的输出。我觉得还可以。
<root type="object">
<DataEvents type="array">
<item type="object">
<__type type="string">IntEvent:#WcfTest.Data</__type>
<Id type="number">12345</Id>
<Timestamp type="string">/Date(1324905383689+0000)/</Timestamp>
<Value type="number">5</Value>
</item>
<item type="object">
<__type type="string">BoolEvent:#WcfTest.Data</__type>
<Id type="number">45678</Id>
<Timestamp type="string">/Date(1324905383689+0000)/</Timestamp>
<Value type="boolean">true</Value>
</item>
</DataEvents>
</root>
在跟踪数据包的反序列化时,我在跟踪中收到以下消息:
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Verbose">
<TraceIdentifier>http://msdn.microsoft.com/en-GB/library/System.Runtime.Serialization.ElementIgnored.aspx</TraceIdentifier>
<Description>An unrecognized element was encountered in the XML during deserialization which was ignored.</Description>
<AppDomain>1c7ccc3b-4-129695001952729398</AppDomain>
<ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/StringTraceRecord">
<Element>:__type</Element>
</ExtendedData>
</TraceRecord>
此消息重复 4 次(两次以__type 作为元素,两次以Value)。看起来类型提示信息被忽略了,然后 Value 元素被忽略,因为数据包被反序列化为 DataEvent 而不是 IntEvent/BoolEvent。
【问题讨论】:
标签: c# wcf json polymorphism