本文介绍了如何设置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?

我有以下服务合同用于处理城市实体。对于设计的原因,市实体有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.

有关获取的方法我可以处理JsonConvert.DeserializeObject的情况,但与PUT,POST,DELETE方法的DataContractSerializer花费precedence和失败抱怨的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.

我觉得这Post实施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.

我想AP preciate任何帮助或指导有关这种情况下,或建议其他办法。

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

感谢

霍山

推荐答案

的扩展恩codeRS和串行器(见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.

如果您已经使用消息键入返回结果的使用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);
}

在发生错误(如的HTTPStatus code.Unauthorized 的HTTPStatus code.Conflict 的),或在其他情况下,当你需要设置一个HTTP状态code(如的HTTPStatus code.Created ),您可以继续使用 WebOperationContext.Current.OutgoingResponse.Status code

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.

另一种方法,你也可以返回(见http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx和http://msdn.microsoft.com/en-us/library/ms732038.aspx)而不是消息返回任何数据,而无需通过微软的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服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:20