作为将 WCF 作为 REST 服务调用的一部分,类型字典的属性之一未被序列化



我已经公开了将WCF调用为休息服务;我能够用复杂对象调用该特定的 Web 服务。但是,字典类型的属性之一未被序列化,并且在涉及 WCF 时变为空。我在代码中提供了更多详细信息。

InFieldValuePair is dictonary

JSON 请求 :

{  
"Requests":[  
{  
"AppRuleGroup":{  
"AppId":0,
"AppName":"XXX",
"SubGroupId":0,
"SubGroupName":"Corporates - Investment Grade",
"GroupId":0,
"GroupName":"Workflow",
"ModuleId":0,
"ModuleName":"Trading",
"RulesLastUpdatedBy":null,
"EvalRules":[  
]
},
"InputRequests":[  
{  
"Guid":"8592080a-6236-4b37-91b5-48c8a988950b",
"InFieldValuePair":{  
"CurrentStatus":"Counter1",
"Direction":"Out"
}
}
],
"Guid":"a0f0fba0-bf3b-4d3d-adc8-416b5448b3df"
}
]
}

大约

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Task<List<RuleEvaluationResponse2>> Evaluate2(List<RuleEvaluationRequest2> Requests);

预期结果是应填充字典对象

首先,请确保对象的属性已由[DataContract]/[DataMember]修饰。
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
此外,如果 T 包含其他对象,为了确保序列化和反序列化正常工作,我们应该考虑使用 [KnownType] 属性。
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-known-types
我们可以使用 IExtensibleDataObject 接口来处理服务器和客户端数据协定不一致的情况,以及未序列化的成员被正确序列化的情况。
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.iextensibledataobject?view=netframework-4.8
最后,当数据项太多时。请考虑使用 MaxItemsInObjectGraph 属性。
https://thinksimpleshirin.wordpress.com/2011/12/06/increasing-maxitemsinobjectgraph-wcf/
如果问题仍然存在,请随时告诉我。

最新更新