如何将 Json.Net 设置为 WCF REST 服务的默认序列化程序 [英] How to set Json.Net as the default serializer for WCF REST service

查看:19
本文介绍了如何将 Json.Net 设置为 WCF REST 服务的默认序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在序列化/反序列化实体时覆盖默认的 WCF DataContractSerializer 行为并改用 JSON.NET?

Is it possible to override the default WCF DataContractSerializer behaviour when Serialize/DeSerialize entities and use JSON.NET instead?

我有以下服务合同来处理 City 实体.由于设计原因,City 实体的 IsReference=true,因此默认的 DataContractSerializer 会引发错误.

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.

对于GET"方法,我可以使用 JsonConvert.DeserializeObject 处理这种情况,但使用PUT、POST、DELETE"方法 DataContractSerializer 优先并且失败,因为 IsReference 实体无法序列化.

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.

我找到了这个帖子 来实现 IOperationBehavior 并提供我自己的序列化程序,但我不知道如何将 Json.NET 与它集成.我相信应该有更直接的方法来解决这个问题.

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)
    {
    }
}

非常感谢

霍萨姆

推荐答案

Extended Encoders 和 Serializers 的用法(参见 http://msdn.microsoft.com/en-us/library/ms733092.aspx) 或其他扩展 WCF 的方法,例如使用 DataContractSerializerOperationBehavior有趣,但对于您的特殊问题,有更简单的解决方法.

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 DataContractSerializerOperationBehavior is very interesting, but for your special problem there are easier solution ways.

如果您已经使用 Message 类型返回结果并使用 WCF4,您可以执行以下操作:

If you already use Message type to return the results an use WCF4 you can do something like following:

public Message UpdateCity(string code, City city)
{
    MyResponseDataClass message = CreateMyResponse();
    // use JSON.NET to serialize the response data
    string myResponseBody = JsonConvert.Serialize(message);
    return WebOperationContext.Current.CreateTextResponse (myResponseBody,
                "application/json; charset=utf-8",
                Encoding.UTF8);
}

如果出现错误(例如 HttpStatusCode.UnauthorizedHttpStatusCode.Conflict)或其他需要设置 HTTP 状态代码的情况(例如 HttpStatusCode.Created) 您可以继续使用 WebOperationContext.Current.OutgoingResponse.StatusCode.

In case of errors (like HttpStatusCode.Unauthorized or HttpStatusCode.Conflict) or in other situations when you need to set a HTTP status code (like HttpStatusCode.Created) you can continue to use WebOperationContext.Current.OutgoingResponse.StatusCode.

作为替代方案,您还可以返回 Stream(参见 http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspxhttp://msdn.microsoft.com/en-us/library/ms732038.aspx) 而不是 Message 以返回任何数据,而无需 Microsoft JSON 序列化程序进行额外的默认处理.在 WCF4 的情况下,您可以使用 CreateStreamResponse(请参阅 http://msdn.microsoft.com/en-us/library/dd782273.aspx) 而不是 CreateTextResponse.如果您将使用此技术生成响应,请不要忘记在写入流后将流位置设置为 0.

As an alternative you can also return a Stream (see http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx and http://msdn.microsoft.com/en-us/library/ms732038.aspx) instead of Message to return any data without additional default processing by Microsoft JSON serializer. In case of WCF4 you can use CreateStreamResponse (see http://msdn.microsoft.com/en-us/library/dd782273.aspx) instead of CreateTextResponse. Don't forget to set stream position to 0 after writing in the stream if you will use this technique to produce the response.

这篇关于如何将 Json.Net 设置为 WCF REST 服务的默认序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆