小编max*_*05b的帖子

将WCF 4中的默认JSON序列化程序替换为JSON.NET

我想用JSON.NET替换默认的WCF JSON(对于所有数据类型)序列化.我在网上搜索过,找不到合适的解决方案.

这是我的目标:

    [JsonObject]
public class TestObject
{
    [JsonProperty("JsonNetName")]
    public string Name = "John";

    [JsonProperty]
    public DateTime Date = DateTime.Now;
}
Run Code Online (Sandbox Code Playgroud)

这是我的WCF功能:

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    List<TestObject> Get();
Run Code Online (Sandbox Code Playgroud)

这是Global.asax中的代码:

        protected void Application_Start(object sender, EventArgs e)
    {
        // Create Json.Net formatter serializing DateTime using the ISO 8601 format
        var serializerSettings = new JsonSerializerSettings();

        serializerSettings.Converters.Add(new IsoDateTimeConverter());
        serializerSettings.Converters.Add(new BinaryConverter());
        serializerSettings.Converters.Add(new JavaScriptDateTimeConverter());
        serializerSettings.Converters.Add(new BinaryConverter());
        serializerSettings.Converters.Add(new StringEnumConverter());

        var config = HttpHostConfiguration.Create().Configuration;

        Microsoft.ApplicationServer.Http.JsonMediaTypeFormatter jsonFormatter = config.OperationHandlerFactory.Formatters.JsonFormatter;

        config.OperationHandlerFactory.Formatters.Remove(jsonFormatter);

        config.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter(serializerSettings));

        var httpServiceFactory = …
Run Code Online (Sandbox Code Playgroud)

c# wcf json json.net c#-4.0

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

c#-4.0 ×1

json ×1

json.net ×1

wcf ×1