我开始这个新线程作为评论的延续: 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/
我在 .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”的来源?)。
编辑:这是来自提琴手的 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"}
这是来自提琴手的响应原始数据:
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"}}
谢谢