Wcf 如何从服务重新序列化数据结构并保持相同的字段名?
我正在编写一个中间层WCF服务,它需要从后端.asmx服务获取一些数据,然后将其作为JSON编码字符串提供给前端 为此,我将对后端服务的引用添加到我的项目中,使用该引用获取数据,然后使用DataContractJsonSerializer重新序列化数据,如下所示:Wcf 如何从服务重新序列化数据结构并保持相同的字段名?,wcf,json,serialization,service,asmx,Wcf,Json,Serialization,Service,Asmx,我正在编写一个中间层WCF服务,它需要从后端.asmx服务获取一些数据,然后将其作为JSON编码字符串提供给前端 为此,我将对后端服务的引用添加到我的项目中,使用该引用获取数据,然后使用DataContractJsonSerializer重新序列化数据,如下所示: using (BackendService.BackendServicesSoapClient c = new BackendService.BackendServicesSoapClient())
using (BackendService.BackendServicesSoapClient c = new BackendService.BackendServicesSoapClient())
{
// Get data from back-end
BackendService.SomeData data = c.GetData();
using (MemoryStream msData = new MemoryStream())
{
// Serialise data to a JSON-encoded string
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(BackendService.SomeData));
serializer.WriteObject(msData, data);
jsonEncodedData = System.Text.Encoding.UTF8.GetString(msData.GetBuffer(), 0, Convert.ToInt16(msData.Length));
}
}
现在,总的来说,这是可行的。但是,JSON编码字符串中的字段名与oringal SomeData类中的字段名不匹配——它们都附加了“field”
e、 g
这似乎与将服务引用添加到项目时自动生成的代理代码有关。如果我查看生成的reference.cs,我会看到数据类的定义如下:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.com/Data/BackEnd/2010/11/")]
public partial class SomeData : object, System.ComponentModel.INotifyPropertyChanged {
private string nameField;
private string locationNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
this.RaisePropertyChanged("Name");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public string Age {
get {
return this.ageField;
}
set {
this.ageField = value;
this.RaisePropertyChanged("Age");
}
}
}
有没有办法让前端和后端的字段名匹配?问题是,在生成代理时,您正在使用XML序列化程序,然后尝试使用DataContract(Json)序列化程序重新序列化。它们使用不同的属性。您的类不是用DataContract属性注释的,而是用Serializable属性注释的 为此,您需要代理类上的DataContract属性,或者需要删除Serializable属性。不幸的是,在使用XmlSerialier时,我不知道如何让生成的代码为您执行这两项操作(因为下游服务是一个.ASMX服务) 为了您自己的理智,最好创建您自己的类,您可以控制该类来创建所需的JSON,将ASMX服务中的数据映射到此类中,然后使用您的类序列化为JSON。这还有一个额外的好处,那就是如果asmx服务因某种原因而改变,它将不会改变;除非您希望,否则不要影响JSON
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.com/Data/BackEnd/2010/11/")]
public partial class SomeData : object, System.ComponentModel.INotifyPropertyChanged {
private string nameField;
private string locationNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
this.RaisePropertyChanged("Name");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public string Age {
get {
return this.ageField;
}
set {
this.ageField = value;
this.RaisePropertyChanged("Age");
}
}
}
public class SomeData
{
public string Name { get; set; }
public string Age { get; set; }
}