Question

Does anyone know whether it is possible to have 2 different endpoints using 2 different Contract Serializers? Particularly, the default DataContractSerializer for SOAP/XML and Json.NET serializer for REST/JSON

The aim (due compatibilities issues) is to have the same DTO class Person below, serialized into the following JSON and XML formats

DTO Class:

public class Person
{
    public string Name { get; set; }
    public string Surname { get; set; }
}

JSON

{Psn:{"Nm":"name1","Snm":"surname1"}}

XML

<Person><Name>name1</Name><Surname>surname1</Surname></Person>

As can be seen, Response for JSON should contain shrunk property names, but full property names for XML version.

My thoughts were to have both serialisation annotations, those understood by the DataContractSerialiser and those understood by Json.Net serializer i.e. something of this sort:

[DataMember]
[JsonObject(Title="Psn")]
public class Person
{
    [DataMember]
    [JsonProperty(PropertyName="Nm")]
    public string Name { get; set; }

    [DataMember]
    [JsonProperty(PropertyName="Snm")]
    public string Surname { get; set; }
}

I would appreciate any thoughts on how this can be achieved.

Était-ce utile?

La solution 2

The way this was finally implemented was using the WCF Raw Method. http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx

Keeping the same DTO mentioned in the question, two operational contracts are created.

One for SOAP/XML GetPersonResponse GetPerson(GetPersonRequest request) and another one for REST/JSON System.IO.Stream GetPerson_Rest(params...).

We would go and implement GetPerson() as required. Then in GetPerson_Rest(), we would call GetPerson() afterwhich the returned GetPersonResponse would be converted into Json using JSON.Net library. Finally the method would return a Stream object, as shown in the following snippet:

string jsonResponse = JsonConvert.SerializeObject(response);

// Serialize into stream of bytes
byte[] rawResponse = Encoding.UTF8.GetBytes(jsonResponse);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";

// Return json raw response
return new MemoryStream(rawResponse);

Autres conseils

One solution would be to have 3 person classes that have the same structure.

[DataMember]
public class Person_dm

[JsonObject(Title="Psn")]
public class Person_json

public class Person

use the Person class internally, the Person_dm and Person_json in their respective contracts / endpoints. You will need to map between the types but this can be done with Automapper.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top