我有一个使用 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
确实包含子类型而不是父类(super class)型。一旦我将请求设置为 text/json
并发送上述数据包,我就只能得到父类(super class)型,而不能将它们转换为子类型。我的 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 的评论后更新了服务描述。在 Fiddler2 中发送 JSON 数据包时仍然不起作用。我只是得到一个包含 List
而不是 DataEvent
和 IntEvent
的 BoolEvent
。
编辑 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
。
最佳答案
每当处理序列化时,尝试先序列化一个对象图以查看序列化后的字符串格式。然后使用该格式生成正确的序列化字符串。
您的数据包不正确。正确的是:
POST http://localhost:47440/Service1.svc/SubmitDataEvents HTTP/1.1
User-Agent: Fiddler
Host: localhost:47440
Content-Length: 211
Content-Type: text/json
[
{
"__type":"IntEvent:#WcfTest.Data",
"Id":12345,
"Timestamp":"\/Date(1324757832735+0700)\/",
"Value":5
},
{
"__type":"BoolEvent:#WcfTest.Data",
"Id":45678,
"Timestamp":"\/Date(1324757832736+0700)\/",
"Value":true
}
]
还要注意 Content-Type
header 。
我已经用你的代码试过了,它工作得很好(好吧,我已经删除了 Console.WriteLine
并在调试器中进行了测试)。所有的类层次结构都很好,所有对象都可以转换为它们的类型。它有效。
更新
您发布的 JSON 使用以下代码:
[DataContract]
public class SomeClass
{
[DataMember]
public List<DataEvent> dataEvents { get; set; }
}
...
[ServiceContract]
public interface IDataService
{
...
[OperationContract]
[WebInvoke(UriTemplate = "SubmitDataEvents")]
void SubmitDataEvents(SomeClass parameter);
}
请注意,对象树中添加了另一个高级节点。
再一次,它与继承一起工作得很好。
如果问题仍然存在,请发布您用于调用该服务的代码,以及您获得的异常详细信息。
更新 2
真奇怪......它在我的机器上工作。
我在 Win7 x64 上使用 .NET 4 和 VS2010 以及最新更新。
我接受你的服务契约(Contract)、实现和数据契约(Contract)。我将它们托管在 Cassini 下的 Web 应用程序中。我有以下 web.config:
<configuration>
<connectionStrings>
<!-- excluded for brevity -->
</connectionStrings>
<system.web>
<!-- excluded for brevity -->
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="WebApplication1.DataService">
<endpoint address="ws" binding="wsHttpBinding" contract="WebApplication1.IDataService"/>
<endpoint address="" behaviorConfiguration="WebBehavior"
binding="webHttpBinding"
contract="WebApplication1.IDataService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
</configuration>
现在我通过 Fiddler2 进行以下 POST(重要:我已重命名派生类型的命名空间以匹配我的情况):
POST http://localhost:47440/Service1.svc/SubmitDataEvents HTTP/1.1
User-Agent: Fiddler
Content-Type: text/json
Host: localhost:47440
Content-Length: 336
{
"DataEvents": [{
"__type": "IntEvent:#WebApplication1",
"Id": 12345,
"Timestamp": "\/Date(1324905383689+0000)\/",
"Value": 5
}, {
"__type": "BoolEvent:#WebApplication1",
"Id": 45678,
"Timestamp": "\/Date(1324905383689+0000)\/",
"Value": true
}]
}
然后我在服务实现中有如下代码:
public void SubmitDataEvents(DataPacket parameter)
{
foreach (DataEvent dataEvent in parameter.DataEvents)
{
var message = dataEvent.ToString();
Debug.WriteLine(message);
}
}
请注意,调试器将项目详细信息显示为 DataEvent
,但字符串表示和详细信息中的第一项清楚地表明所有子类型都已反序列化:
在我点击该方法后,调试输出包含以下内容:
IntEvent: 12345, 26.12.2011 20:16:23, 5
BoolEvent: 45678, 26.12.2011 20:16:23, True
我也试过在 IIS 下运行它(在 Win7 上),一切正常。
在我通过从 __type
字段名称中删除一个下划线来破坏数据包后,我只反序列化了基本类型。如果我修改 __type
的值,调用将在反序列化期间崩溃,它不会命中服务。
以下是您可以尝试的方法:
- 确保您没有任何调试消息、异常等(检查调试输出)。
- 创建一个新的干净的网络应用程序解决方案,粘贴所需的代码并测试它是否在那里工作。如果是这样,那么您的原始项目一定有一些奇怪的配置设置。
- 在调试器中,分析监 window 口中的
System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.ToString()
。它将包含从您的 JSON 翻译而来的 XML 消息。检查是否正确。 - 检查您是否有任何待处理的 .NET 更新。
- 尝试 tracing WCF 。尽管它似乎不会针对错误的
__type
字段名称的消息发出任何警告,但它可能会针对您的问题原因向您显示一些提示。
我的请求消息
这似乎是问题的根源:虽然您将 __type
作为元素,但我将其作为属性。据推测,您的 WCF 程序集在 JSON 到 XML 的转换中存在错误
<root type="object">
<DataEvents type="array">
<item type="object" __type="IntEvent:#WebApplication1">
<Id type="number">12345</Id>
<Timestamp type="string">/Date(1324905383689+0000)/</Timestamp>
<Value type="number">5</Value>
</item>
<item type="object" __type="BoolEvent:#WebApplication1">
<Id type="number">45678</Id>
<Timestamp type="string">/Date(1324905383689+0000)/</Timestamp>
<Value type="boolean">true</Value>
</item>
</DataEvents>
</root>
我找到了处理__type
的地方。在这里:
// from System.Runtime.Serialization.Json.XmlJsonReader, System.Runtime.Serialization, Version=4.0.0.0
void ReadServerTypeAttribute(bool consumedObjectChar)
{
int offset;
int offsetMax;
int correction = consumedObjectChar ? -1 : 0;
byte[] buffer = BufferReader.GetBuffer(9 + correction, out offset, out offsetMax);
if (offset + 9 + correction <= offsetMax)
{
if (buffer[offset + correction + 1] == (byte) '\"' &&
buffer[offset + correction + 2] == (byte) '_' &&
buffer[offset + correction + 3] == (byte) '_' &&
buffer[offset + correction + 4] == (byte) 't' &&
buffer[offset + correction + 5] == (byte) 'y' &&
buffer[offset + correction + 6] == (byte) 'p' &&
buffer[offset + correction + 7] == (byte) 'e' &&
buffer[offset + correction + 8] == (byte) '\"')
{
// It's attribute!
XmlAttributeNode attribute = AddAttribute();
// the rest is omitted for brevity
}
}
}
我试图找到使用属性确定反序列化类型的地方,但没有成功。
希望这对您有所帮助。
关于c# - 使用 JSON 在 WCF 服务中保留多态类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8625803/