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

max*_*05b 13 c# wcf json json.net c#-4.0

我想用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 = new HttpServiceHostFactory
        {
            OperationHandlerFactory = config.OperationHandlerFactory,
            MessageHandlerFactory = config.MessageHandlerFactory
        };

        //Routing
        RouteTable.Routes.Add(
           new ServiceRoute(
               "Brands", httpServiceFactory,
               typeof(Brands)));

      }
Run Code Online (Sandbox Code Playgroud)

这是Web.Config:

 <endpointBehaviors>
    <behavior name="Behavior_Brands">
      <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare" />
    </behavior>
  </endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)

和服务部分:

<service name="TestApp.CoreWCF.Brands">
    <endpoint address="" behaviorConfiguration="Behavior_Brands" binding="webHttpBinding" contract="TestApp.CoreWCF.IBrands">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
  </service>
Run Code Online (Sandbox Code Playgroud)

最后,这是我在启动URL时得到的:

" http:// localhost:30000/Brands/Get "

[{"Date":"\/Date(1354364412708+0200)\/","Name":"John"}, {"Date":"\/Date(1354364412708+0200)\/","Name":"John"}]
Run Code Online (Sandbox Code Playgroud)

JSON答案显然忽略了JSON.NET属性.

max*_*05b 19

无论如何,我想出了一种方法来手动使用不同的序列化器,它似乎更高效,更快,因为它不通过微软的串行器,虽然代码方面它有点麻烦.

  1. 在接口和实现它们的类中将所有返回类型设置为"System.ServiceModel.Channels.Message".

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    System.ServiceModel.Channels.Message GetData(); 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建一个扩展方法,以便您可以使用JSON.NET序列化程序(或您想要使用的任何一个)轻松地从对象构建内存流.

    public static System.ServiceModel.Channels.Message GetJsonStream(this object obj)
    {
        //Serialize JSON.NET
        string jsonSerialized = JsonConvert.SerializeObject(obj);
    
        //Create memory stream
        MemoryStream memoryStream = new MemoryStream(new UTF8Encoding().GetBytes(jsonSerialized));
    
        //Set position to 0
        memoryStream.Position = 0;
    
        //return Message
        return WebOperationContext.Current.CreateStreamResponse(memoryStream, "application/json");
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在方法的主体中,将序列化的对象直接返回到流

    return yourObject.GetJsonStream();
    
    Run Code Online (Sandbox Code Playgroud)

  • 有哪些测试结果表明它肯定更快?不幸的是,对于我的测试用例(500KB对象层次结构)使用类似的替代技术,看起来JSON.NET序列化_plus_字节数组复制到UTF8的双重步骤使得它比普通的DataContractJsonSerializer更昂贵.:-( (4认同)