Is it possible to override the default WCF DataContractSerializer behaviour when Serialize/DeSerialize entities and use JSON.NET instead?
I have the following service contract for handling the City entity. For design reasons the City entity has IsReference=true, and therefore the default DataContractSerializer raise errors.
For the “GET” methods I can handle the situation with JsonConvert.DeserializeObject, but with “PUT,POST,DELETE” methods DataContractSerializer takes precedence and fails complaining for the IsReference entities cannot be serialized.
I have find this Post to implement IOperationBehavior and provide my own Serializer but I do not know how to integrate Json.NET with this. and I believe there should be more straight forward approach for this.
I’d appreciate any help or guidance regarding this scenario, or advice to other approaches.
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class CityService
{
[Description("Get all Cities")]
[WebGet(UriTemplate = "")]
public Message Cities()
{
}
[Description("Allows the details of a single City to be updated.")]
[WebInvoke(UriTemplate = "{code}", Method = "PUT")]
public Message UpdateCity(string code, City city)
{
}
}
Many thanks
Hossam
The usage of Extending Encoders and Serializers (see http://msdn.microsoft.com/en-us/library/ms733092.aspx) or other methods of Extending WCF like usage of
DataContractSerializerOperationBehavioris very interesting, but for your special problem there are easier solution ways.If you already use
Messagetype to return the results an use WCF4 you can do something like following:In case of errors (like
HttpStatusCode.UnauthorizedorHttpStatusCode.Conflict) or in other situations when you need to set a HTTP status code (likeHttpStatusCode.Created) you can continue to useWebOperationContext.Current.OutgoingResponse.StatusCode.As an alternative you can also return a
Stream(see Link and http://msdn.microsoft.com/en-us/library/ms732038.aspx) instead ofMessageto return any data without additional default processing by Microsoft JSON serializer. In case of WCF4 you can useCreateStreamResponse(see http://msdn.microsoft.com/en-us/library/dd782273.aspx) instead ofCreateTextResponse. Don’t forget to set stream position to 0 after writing in the stream if you will use this technique to produce the response.