用于 WCF REST 服务的 JSON.NET 序列化程序 [英] JSON.NET Serializer for WCF REST Services

查看:16
本文介绍了用于 WCF REST 服务的 JSON.NET 序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NETFx Json.NET MediaTypeFormatter nuget 包进行交换在我的 WCF REST 服务(4.0 框架)中删除默认的 DataContractJsonSerializer.我在我的项目中下载了这个包,并在 Global.asax 文件中添加了以下代码行.

I am trying to use the NETFx Json.NET MediaTypeFormatter nuget package to swap out the default DataContractJsonSerializer in my WCF REST service (4.0 framework). I downloaded the package in my project and added the following lines of code in the Global.asax file.

    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();

        // Create Json.Net formatter serializing DateTime using the ISO 8601 format
        var serializerSettings = new JsonSerializerSettings();
        serializerSettings.Converters.Add(new IsoDateTimeConverter());

        var config = HttpHostConfiguration.Create();
        config.Configuration.OperationHandlerFactory.Formatters.Clear();
        config.Configuration.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter(serializerSettings));
    }

但是当我运行该服务时,它仍然使用 DataContractJsonSerilizer 进行序列化.下面是我从服务中返回的课程.

But when I run the service it still uses the DataContractJsonSerilizer for serialization. Below is the class I am returning from my service.

[DataContract]
public class SampleItem
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string StringValue { get; set; }

    [DataMember]
    public DateTime DateTime { get; set; }
}

以下是 Fiddler 中服务的响应.

Below is the response from the service in Fiddler.

您可以看到 DateTime 不是我在上面代码的 serializerSettings 中指定的 ISO 格式.这告诉我 JSON.NET 序列化程序不用于序列化对象.

You can see that the DateTime is not in ISO format which I have specified in serializerSettings in the above code. This tells me that the JSON.NET serializer is not used for serializing the objects.

不胜感激.

推荐答案

想通了答案后,我觉得自己很笨.有时会发生:).我必须将配置添加到 RouteTable.下面是 Global.asax 中的代码

I feel dumb after I figured the answer. Happens at times :). I had to add the config to the RouteTable. Below is the code in Global.asax

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        // Create Json.Net formatter serializing DateTime using the ISO 8601 format
        var serializerSettings = new JsonSerializerSettings();
        serializerSettings.Converters.Add(new IsoDateTimeConverter());

        var config = HttpHostConfiguration.Create().Configuration;
        config.OperationHandlerFactory.Formatters.Clear();
        config.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter(serializerSettings));

        var httpServiceFactory = new HttpServiceHostFactory
                                     {
                                         OperationHandlerFactory = config.OperationHandlerFactory,
                                         MessageHandlerFactory = config.MessageHandlerFactory
                                     };

        RouteTable.Routes.Add(new ServiceRoute("Service1", httpServiceFactory, typeof(Service1)));
    }
}

希望如果他们碰巧遇到同样的情况,它会有所帮助.

Hope it will help somebody if they happen to run into the same scenario.

这篇关于用于 WCF REST 服务的 JSON.NET 序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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