【问题标题】:Posting to a WCF Data Service 5发布到 WCF 数据服务 5
【发布时间】:2012-06-26 16:54:16
【问题描述】:

我正在尝试 POST 到 EntityFramework 支持的 WCF 数据服务,但我得到:

415 Unsupported Media Type

我按照本指南将标题放入我的 jQuery POST http://blogs.msdn.com/b/astoriateam/archive/2012/04/11/what-happened-to-application-json-in-wcf-ds-5-0.aspx

这些是我的标题:

POST /webservices/service/service.svc/Activities HTTP/1.1
Host: www.url.com
Connection: keep-alive
Content-Length: 138
Origin: http://www.url.com
X-Requested-With: XMLHttpRequest
MaxDataServiceVersion: 3.0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5
Content-Type: application/json; charset=UTF-8
Accept: application/json;odata=verbose
Referer: http://www.url.com/sites/site/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

本网站帮助将 EntityFramework 与 WCF 数据服务结合使用 - http://blogs.msdn.com/b/writingdata_services/archive/2011/06/15/entity-framework-4-1-code-first-and-wcf-data-services.aspx

function AddActivity() {
    var activity = {
        activity:
            {
                "Title": "Test From Code",
                "Detail": "Code Example",
                "Started": "2012-06-21T09:00:00",
                "UserId": 17
            }
    };

    var url = 'http://www.url.com/webservices/service/service.svc/Activities';

    $.ajax({
        type: "POST",
        url: url,
        data: activity,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Accept", "application/json;odata=verbose");
            xhr.setRequestHeader("MaxDataServiceVersion", "3.0");
        },
        success: function (data) {
            alert('Success');
        },
        error: function (err) {
            alert('Fail\n' + err.statusText);
        }
    });
}

我可以读取数据JSON数据OK

【问题讨论】:

    标签: json entity-framework wcf-data-services


    【解决方案1】:

    如果负载具有版本 V3 (DataServiceVersion),WCF 数据服务 5.0 不支持请求(或响应)负载的 application/json 内容类型。在上述情况下,您没有指定 DataServiceVersion (顺便说一句,您总是应该这样做)。在这种情况下,服务器必须以某种方式推断版本,WCF 数据服务服务器将假定它和客户端理解的最大版本。由于服务器本身理解 V3,并且您指定了 MaxDataServiceVersion: 3.0,这意味着客户端也理解 V3,因此它假定有效负载是 V3 有效负载。

    V3 有效负载目前不支持 application/json,它们仅支持 application/json;odata=verbose。

    有关详细说明,请参阅此博客文章:http://blogs.msdn.com/b/astoriateam/archive/2012/04/11/what-happened-to-application-json-in-wcf-ds-5-0.aspx。它谈到了 GET,但同样的事情也适用于 POST 等。

    因此,要解决您的问题,请修改您的客户端以发送 DataServiceVersion: 2.0(或 1.0)(如果正确),或者更好地修改它以发送 Content-Type: application/json;odata=verbose(这将起作用无论负载版本如何)。

    【讨论】:

      【解决方案2】:

      只是用最少的黑客攻击做到了这一点,使用没有任何 WCF 数据服务组件的 WCF。

      将最小类型返回为 JSON 将自动映射值,例如,此 JSON

      {
          "name": "1234567890",
          "amount": "2500.00"
      }
      

      可以发回此服务

      [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json)]
      public string MethodName(Message message)
      

      其中 Message 是 C# 类:

      public class Message
      {
          public string name { get; set; }
          public string amount { get; set; }
      }
      

      很简单。然后,如果您想将其作为 OData 请求处理,您将发布更复杂的 JSON,例如 (ODataV2):

      {
      "d": [
          {
              "__metadata": {
                  "id": "http://host:54470/ods.svc/MethodName('1234567890')",
                  "uri": "http://host:54470/ods.svc/MethodName('1234567890')",
                  "type": "SomeCompany.Helper.Services.RequestMessage.Message"
              },
              "name": "1234567890",
              "amount": "2500.00",
          }
      ]
      }
      

      如果您创建相应的 C# 类,此 OData 将映射到这些类。这些类与 OData 一起使用。

      public class Message
      {
          public ODataResult[] d;
      }
      
      public class ODataResult
      {
          public ODataMetaData __metadata;
          public string name { get; set; }
          public string amount { get; set; }
      }
      
      public class ODataMetaData
      {
          public string id;
          public string uri;
          public string type;
      }
      

      json 发布的 OData 将映射到这个 Message 类。这为您提供了一种根据需要“塑造”传入消息的方法,这在某些情况下可能是必需的。

      并不是说这比使用 WCF 数据服务更好,只是说它会起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        • 1970-01-01
        • 2011-07-31
        相关资源
        最近更新 更多