无法使用 wcf 客户端服务 5.3 生成器解析包含根属性"d"的 odata json 响应



我开始这个新的线程作为评论的延续:使用Odata服务并在JSON中获取结果

我面临的问题是,我已经升级到wcf数据服务5.5和wcf客户端工具5.3在线程中推荐。我正在尝试执行一个简单的帖子到以下JayStorm服务:https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/

我在。net中生成了客户端服务引用,并运行以下代码:

using Microsoft.Data.Edm;
using Microsoft.Data.Edm.Csdl;
using Microsoft.Data.Edm.Validation;
using Microsoft.Data.OData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Spatial;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace AirportDBDataImporter
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = "https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/";
            var db = new AirportDB.mydatabaseService(new Uri(url));
            var xmlTextReader = new XmlTextReader(url+"$metadata");
            IEdmModel edmModel = null;
            IEnumerable<EdmError> errors = null;
            if (EdmxReader.TryParse(xmlTextReader, out edmModel, out errors))
            {
            }
            db.Format.UseJson(edmModel);
            //{"Name":"sfd","Abbrev":"sd","GeoLocation":{"type":"Point","coordinates":[-71.56236648559569,42.451074707889646],"crs":{"properties":{"name":"EPSG:4326"},"type":"name"}}}
            var airport = new AirportDB.Airport();
            airport.Abbrev = "Foo";
            airport.Name = "Bar";
            airport.GeoLocation = GeographyPoint.Create(51.87796, -176.64603);
            db.AddToAirport(airport);
            db.SaveChanges();
            //var foo = db.Airport.ToList();

        }
    }
}

EdmxReader部分是必要的,因为没有它和无参数的UseJson()会抛出异常,因为服务不完全符合odata v3。使用这种方法SaveChanges()仍然抛出一个异常,但是机场记录实际上被插入到数据库中,并且为记录返回的信息(来自JayStorm),因为它包含老派风格的根"d"属性,导致解析异常,并且该异常在SaveChanges()的第二部分抛出。

我的问题是:为了完全完成JayStorm的帖子,我能做些什么吗?这似乎不是因为新的wcf客户端不再支持旧的详细json(我认为这就是"d"的来源?)。

编辑:以下是来自fiddler的POST原始数据:

POST https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/Airport HTTP/1.1
DataServiceVersion: 3.0;NetFx
MaxDataServiceVersion: 3.0;NetFx
Content-Type: application/json;odata=minimalmetadata
Accept: application/json;odata=minimalmetadata
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services
Host: open.jaystack.net
Content-Length: 196
Expect: 100-continue
{"odata.type":"mydatabase.Airport","Abbrev":"Foo","GeoLocation":{"type":"Point","coordinates":[-176.64603,51.87796],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}},"id":null,"Name":"Bar"}

以下是fiddler的响应原始数据:

HTTP/1.1 201 Created
Server: nginx/1.4.1
Date: Fri, 21 Jun 2013 15:07:40 GMT
Content-Type: application/json;odata=verbose;charset=utf-8;charset=UTF-8
Content-Length: 574
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: open.jaystack.net
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type, MaxDataServiceVersion, DataServiceVersion, Authorization, X-Domain, X-Requested-With
Access-Control-Allow-Method: POST
Access-Control-Allow-Methods: OPTIONS, GET, HEAD, POST, MERGE, PATCH, DELETE, PUT
Access-Control-Allow-Credentials: true
location: https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/Airport('NTFjNDZjM2MyMjg1Y2FiNjMzMDAwMDAx')
Set-Cookie: connect.sid=s%3AvwHQXjoJQO3VUxJdE2jrQ3ja.A4tG9Bv4XTg1gS5xAVgxMyWXJYrV6DULf3xWvj1Uhq8; Path=/; HttpOnly
{"d":{"__metadata":{"type":"mydatabase.Airport","id":"https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/Airport('NTFjNDZjM2MyMjg1Y2FiNjMzMDAwMDAx')","uri":"https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/Airport('NTFjNDZjM2MyMjg1Y2FiNjMzMDAwMDAx')"},"Name":"Bar","Abbrev":"Foo","GeoLocation":{"type":"Point","coordinates":[-176.64603,51.87796],"crs":{"properties":{"name":"EPSG:4326"},"type":"name"}},"id":"NTFjNDZjM2MyMjg1Y2FiNjMzMDAwMDAx"}}

谢谢

正如在另一个线程的评论中所讨论的,您的服务器不支持V3的OData,并且当您请求新的JSON格式时,它似乎也没有适当地失败。在请求有效负载中,您说您只理解application/json;odata=minimalmetadata,但服务器忽略了odata=minimalmetadata约束,并以application/json;odata=verbose响应。

你在控制服务器吗?我将研究如何将其升级到OData v3。

正如您已经指出的,WCF Data Services客户端不能使用JSON Verbose。问题不仅在于"d"包装器;这两种格式之间有很多不同之处,WCF DS客户端根本无法理解旧格式(而且从来都无法理解)。

如果可能的话,我强烈建议升级服务器。如果无法升级服务器,仍然可以将Atom与WCF Data Services客户机和v2服务器一起使用。这将意味着更多的字节传输,但如果您关心的是减少网络上的有效负载大小,那么就更有理由升级服务器并使用新的JSON格式,它比旧格式简洁得多。

相关内容

  • 没有找到相关文章

最新更新