I’m writing a middle-tier WCF service that needs to fetch some data from a back-end .asmx service then serve it up to the front-end as a JSON-encoded string.
I do this by adding a reference to the back-end service to my project, fetching data using that reference, then using a DataContractJsonSerializer to re-serialize the data, like this:
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));
}
}
Now, on the whole this works. However, the field names in the JSON-encoded string don’t match those in the oringal SomeData class – they all have “Field” appended.
e.g.
contents of BackendService.Somedata:
Name : Joe
Age : 33
contents of JSON-encoded string
nameField : Joe
ageField : 33
It seems to be something to do with the proxy code that’s auto-generated when I add the service reference to my project. If I look in the generated reference.cs I see definitions like this for the data class:
/// <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");
}
}
}
as compared to the class defined in the back-end service source as:
public class SomeData
{
public string Name { get; set; }
public string Age { get; set; }
}
Is there any way that I can have the field names on the front-end and back-end match?
The problem is that you are using the XML Serializer when generating the proxy and then trying to re-serialize with the DataContract(Json)Serializer. These use different attributes. Your class is not annotated with the DataContract attribute but rather with the Serializable attribute
For this to work you either need the DataContract attribute on the proxy class or you need to remove the Serializable attribute. Unfortunately off the top of my head I’m not sure how you can get the generated code to do either of these for you as you are using the XmlSerialier (because the downstream service is an .ASMX service).
For your own sanity its probably a good idea to create your own class that you control to create the JSON you require, map the data from the ASMX service into this class and then serialize to JSON using your class. This has the added benefit that if the asmx service changed for some reason it wouldn;t affect the JSON unless you wanted it to