无法解析包含根属性"d"的odata json响应.使用WCF客户端服务5.3生成器 [英] Unable to parse odata json responses containing root property "d" using wcf client services 5.3 generator

查看:121
本文介绍了无法解析包含根属性"d"的odata json响应.使用WCF客户端服务5.3生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以以下形式继续发表新话题: 使用Odata服务并在JSON中获取结果

I am starting this new thread as a continuation of comments in: Consume Odata Service and get result in JSON

我面临的问题是我已按照该线程的建议升级到wcf数据服务5.5和wcf客户端工具5.3.我正在尝试对以下JayStorm服务执行简单的发布: https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/

The issue that I am facing is that I have upgraded to wcf data services 5.5 and wcf client tools 5.3 as recommended in the thread. And I am trying to perform a simple post to the following JayStorm service: https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/

我在.Net中生成了客户端服务引用,并且正在运行以下代码:

I generated the client service reference in .Net and am running the following code:

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()的第二部分中引发了异常.

The EdmxReader part is necessary because without it and with a no parameter UseJson() an exception is thrown because the service is not fully odata v3 compliant. With this approach SaveChanges() still throws an exception but the airport record is actually inserted in to the database and the returned information for the record (from JayStorm), because it contains the old school style root "d" property, causes a parse exception and the exception is thrown in the second part of the SaveChanges().

我的问题是:为了完全完成JayStorm的发布,我可以对此做任何事情吗?似乎不是因为新的wcf客户端不再支持旧的冗长的json(我认为这是"d"的来源?).

My question is: Is there anything I can do about this in order to fully complete the post to JayStorm? It seems not as the new wcf client no longer supports the old verbose json (which I think is where the "d" comes from?).

这是来自提琴手的POST原始数据:

Here is the POST raw data from fiddler:

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"}

以下是来自提琴手的响应原始数据:

Here is the response raw data from 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"}}

谢谢

推荐答案

如另一个线程的注释中所述,您的服务器不支持OData的V3,并且当您请求时它似乎也没有发生适当的故障.新的JSON格式.在请求有效负载中,您说的是您只了解application/json;odata=minimalmetadata,但是服务器忽略了odata=minimalmetadata约束,并且始终以application/json;odata=verbose进行响应.

As discussed in the comments of the other thread, your server does not support V3 of OData, and it also doesn't seem to be failing appropriately when you request the new JSON format. In the request payload, you're saying that you only understand application/json;odata=minimalmetadata, but the server is ignoring the odata=minimalmetadata constraint and is responding with application/json;odata=verbose anyway.

您在控制服务器吗?我将研究如何升级到OData v3.

Are you in control of the server? I would look into how that can be upgraded to work with v3 of OData.

您已经指出,WCF数据服务客户端无法与JSON Verbose一起使用.问题不仅仅在于"d"包装器.两种格式之间存在大量差异,并且WCF DS客户端根本无法理解旧格式(而且从来没有能够).

As you already pointed out, the WCF Data Services client won't work with JSON Verbose. The problem is not just the "d" wrapper; there are tons of differences between the two formats, and the WCF DS Client simply can't understand the old format (and never has been able to).

如果有可能,我强烈建议升级服务器.如果无法升级服务器,则仍可以将Atom与WCF数据服务客户端和v2服务器一起使用.这将意味着网络上有更多的字节,但是如果您要担心减少网络上的有效负载大小,那么这就是升级服务器并使用新的JSON格式(比旧格式更简洁)的更多原因.

If it's at all possible, I strongly recommend upgrading the server. If you can't upgrade the server, you can still use Atom with the WCF Data Services client and a v2 server. It'll mean more bytes on the wire, but if reducing payload size on the wire is something you're concerned about, that's even more reason to upgrade the server and use the new JSON format, which is much more concise than the old format.

这篇关于无法解析包含根属性"d"的odata json响应.使用WCF客户端服务5.3生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆