JSON.NET Serializer for WCF REST Services

寵の児 提交于 2019-12-28 02:59:25

问题


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

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; }
}

Below is the response from the service in Fiddler.

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.

Would appreciate any help.


回答1:


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.



来源:https://stackoverflow.com/questions/9827933/json-net-serializer-for-wcf-rest-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!