带有JSON.Net的WCF RESTful Web服务:避免双重序列化

问题描述:

长时间的追踪者,这里是第一次发布者. 我已经为这个问题努力了几天,请多多指教. 我在下面将问题分解.

Long time lurcher, first time poster here. I've been grappling with this problem for a few days now and would appreciate any tips. I break down the issue here below.

我要实现的目标:
我想设置一个JSON WCF Web服务,但是我想使用JSON.net序列化器,而不是WCF附带的序列化器.为什么?因为我发现使用WCF附带的序列化序列会使集合变大(我将在下面显示我的意思的示例).该服务的最终用户将是一个JavaScript客户端,所以我不想让客户端做更多的事情来浏览JSON.

What I'm trying to achieve:
I want to set up a JSON WCF web service but I want to use the JSON.net serializer instead of the one that comes with WCF. Why? Because I found that serializing using the one that comes with WCF bloats up collections (I will show an example of what I mean below). The end consumer of the service is going to be a JavaScript client, so I don't want to have the client do extra messing around to navigate through the JSON.

我想出了一个简单的示例,说明了我想要的东西,并着手尝试使其在C#中工作.我必须承认,很长一段时间都没有使用过.NET,这可能会导致我的速度变慢.无论如何,我都有耐心等待库存.

I came up with a simple example of what I wanted and set out to try to make it work in C#. I must admit to not having used .NET in a long time, which perhaps contributes to my slowness. Anyway, I've got the patience in stock so on with it.

因此,我想要一个接受用户名并返回带有有关用户一些信息的JSON对象的服务. 我设想将该服务称为这样的名称:

So, I want a service that accepts a username and returns a JSON object with some info on the user. I envisaged the service to be called something like this:

http://someHostName/whoareyou?username=paul

让服务对此做出响应:

{
    "errorCode": 0,
    "payLoad"  :  {
        "userFullName": "Paul The Octopus",
        "userLevel"        : "Administrator"
    }
}

我可以轻松地在JavaScript或PHP中使用上述响应的JSON对象,对自己感觉很好.

I could use the above response's JSON object in JavaScript or PHP easily and feel really good about myself.

经过一段时间的搜索后,我发现一些帖子暗示使用.NET Framework 4中的WCF可以轻松实现.我前一阵子曾对WCF进行过摆弄,但早已忘记了它的99%,所以我找到并关注了此帖子以适应我的目标: http://www. codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide#

After some googling around I came across some posts suggesting this would be easy to do with WCF in .NET Framework 4. I had done some fiddling with WCF at a course a while ago but had long forgotten 99% of it so I found and followed this post to adapt to my goal: http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide#

尝试1:
在上面的文章之后,我能够设置一个WCF服务(下面列出的代码),该服务可以实现我想要的功能,但是所产生的JSON输出有点肿,如下所示.

Attempt take 1:
Following the above post I was able to set up a WCF Service (code set out below) that did what I wanted but the resulting JSON output was a bit bloated, as you can see below.

RestServiceImpl.svc.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Specialized;
//Based on this psot: http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

namespace RestService
{
    public class RestServiceImpl : IRestServiceImpl
    {
        #region IRestService Members

        public WhoAreYouResponse whoareyou(string username)
        {
            var payLoad = new Dictionary<string, string>
            {
                {"userFullName", "Paul The Octopus"},
                {"userLevel", "Administrator"}
            };

            WhoAreYouResponse whoAreYouResponse = new WhoAreYouResponse
            {
                errorCode = 0,
                payLoad   = payLoad
            };

            return whoAreYouResponse;
        }

        #endregion
    }

    //Helper bits to be used in service implementation
    public class WhoAreYouResponse
    {
        public int errorCode { get; set; }
        public Dictionary<string,string> payLoad { get; set; }
    }
}

IRestServiceImpl.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestService
{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET", 
            ResponseFormat = WebMessageFormat.Json, 
            BodyStyle = WebMessageBodyStyle.Bare, 
            UriTemplate = "json/whoareyou?username={username}")]
        WhoAreYouResponse whoareyou(string username);
    }
}

Web.config文件

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

*通过这样的http://192.168.0.84/TestRestService/RestServiceImpl.svc/json/whoareyou?username=paul*

{
    "errorCode":0,
    "payLoad"   : [
        {"Key":"userFullName", "Value":"Paul The Octopus"},
        {"Key":"userLevel","Value":"Administrator"}
    ]
}

JSON响应中汤中的头发是在JSON响应中将Dictionary对象映射到"payLoad"的方式.它是一个对象数组,而我期望一个JSON对象解决这个键",值"业务.不完全是我想要的.封闭,但在客户端处理时有点挑剔.没有人喜欢膨胀和不必要的工作,所以请对此表示反对.

The hair in the soup in the JSON response is the way the Dictionary object has been mapped out in the JSON response to "payLoad". It's an array of objects, whereas I was expecting a JSON object sans this "Key", "Value" business. Not quite what I wanted. Close, but pretty finicky to handle at the client end. No one likes bloat and unnecessary work, so thumbs down to this.

更多地寻找解决方案,我读了一些SO帖子,暗示这里发生的事情是该服务正在使用内置的序列化程序 并且不会序列化字典以任何其他方式".要获得我期望的格式,需要做一些额外的工作. 所以,我想,使用另一个串行器怎么样? 遵循这个想法,我在这里发现了这个坏男孩: http://james.newtonking.com /projects/json-net.aspx

Doing some more trolling around for solutions I read some SO posts suggesting what's happening here is that the service is using the built-in serializer and that doesn't serialize "dictionaries in any other way". Some extra work would be needed to get it in the format I was expecting. So, I thought, how about using another serializer? Following this thought I found out about this bad boy here: http://james.newtonking.com/projects/json-net.aspx

这导致我在下面进行第二次尝试.

This lead to my second attempt below.

尝试2:
将JSON.NET序列化程序下载并导入到我的小项目中,我更改了以下文件,使其看起来像这样(将 whoareyou 方法的返回类型更改为 string ):

Attempt take 2:
Downloading and importing the JSON.NET serializer into my little project, I altered the following files to look like this (changing the return type of the whoareyou method to string):

RestServiceImpl.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Specialized;
//Based on this psot: http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

namespace RestService
{
    public class RestServiceImpl : IRestServiceImpl
    {
        #region IRestService Members

        public string whoareyou(string username)
        {
            var payLoad = new Dictionary<string, string>
            {
                {"userFullName", "Paul The Octopus"},
                {"userLevel", "Administrator"}
            };

            WhoAreYouResponse whoAreYouResponse = new WhoAreYouResponse
            {
                errorCode = 0,
                payLoad   = payLoad
            };

            return JsonConvert.SerializeObject(whoAreYouResponse);
        }

        #endregion
    }

    //Helper bits to be used in service implementation
    public class WhoAreYouResponse
    {
        public int errorCode { get; set; }
        public Dictionary<string,string> payLoad { get; set; }
    }
}

IRestServiceImpl.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestService
{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET", 
            ResponseFormat = WebMessageFormat.Json, 
            BodyStyle = WebMessageBodyStyle.Bare, 
            UriTemplate = "json/whoareyou?username={username}")]
        string whoareyou(string username);
    }
}

当服务被调用时,我得到以下响应:

And when the service is called I get this response:

"{
\"errorCode\":0,
\"payLoad\":{
    \"userFullName\":\"Paul The Octopus\",
    \"userLevel\":\"Administrator\"}
}"

获胜者!那就是我想要和期望的……但是.倒退卡车.整个JSON对象用双引号引起来! 嗯这是一个包含JSON对象的字符串.我从汤中取出头发,现在有一只苍蝇飞了进去!

Winner! That's what I wanted and expected….BUT. Back the truck up. The entire JSON object is enclosed in double quotes!? Hmmm. This is a string containing a JSON object. I took the hair out of the soup and now a fly's flown into it!

经过一会儿的抓挠,很明显(我认为)正在发生的事情是JSON.NET序列化器的运行就像一个咒语,将一个JSON对象分散在一个字符串中,然后将其放入WCF序列化器中,本质上是字符串化的.所以我在返回中看到的是一个JSON字符串.很近!实际上,我的方法 whoareyou 说它返回一个字符串,这几乎是我的错.

After a moment's head scratching it became obvious (I think) that what's happening is that the JSON.NET serializer is working like a charm, spitting out a JSON object in a string, but that is then put through the WCF serializer and is essentially stringified. So what I see in the return is a JSON string. So close! In fact, my method whoareyou says that it returns a string, so pretty much my fault.

所以,我的问题是,如何让这个有问题的孩子停止这项双重序列化业务?我找不到 whoareyou 方法的返回类型,就像JSON对象一样. 有没有一种方法可以告诉WCF使用JSON.NET序列化程序,或者某种类似的解决方案?

So, my question is, how do I get this problem child to stop this double-serialization business? I can't find a return type for my whoareyou method to be something like JSON object. Is there a way of telling WCF to use the JSON.NET serializer instead, or some such solution?

非常感谢指针.

据我了解,您是从头开始创建服务,并且对实现REST服务的方式没有任何限制.然后,我建议您使用ASP.NET WebApi

As I understand you are creating service from scratch and there are no limitations on how REST service will be implemented. Then I suggest you to use ASP.NET WebApi

http://www.asp.net/web-api

请勿使用旧版Web服务技术,因为在较新的版本中,已经为您完成了许多样板代码.

Do not use legacy web services technologies because in newers a lot of boilerplate code is already done for you.

使用Web API,您可以轻松替换或添加任何序列化器/格式器.以下文章介绍了如何执行此操作:

With web api you can easily replace or add any serializer/formatter. How to do it is described in following articles:

http://www.asp. net/web-api/overview/formats-and-model-binding/media-formatters

http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization

我遇到了序列化的问题,您已经描述了这种方法并解决了这些问题.根据我在较早的Web服务技术(WCF 3.5 Rest Starter Kit, Wcf 4 REST)可以用更困难的方式完成.

I've experienced problems with serialization that you've described and solved them with this approach. From my experience on older web services technologies ( WCF 3.5 Rest Starter Kit, Wcf 4 REST) it could be done in much harder way.