我正在尝试将示例 v4 OData 服务http://services.odata.org/V4/OData/OData.svc/与新的 OData v7 一起使用。当我打电话ODataMessageReader.ReadMetadataDocument
时,我遇到了一个UnexpectedXmlAttribute
例外。是否可以忽略不支持的属性和元素?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Microsoft.OData;
using Microsoft.OData.Edm;
namespace ConsoleApplication19
{
public class ODataResponseMessage : IODataResponseMessage
{
private readonly Dictionary<string, string> headers;
private WebResponse _response;
public ODataResponseMessage(WebResponse response)
{
this.headers = new Dictionary<string, string>();
_response = response;
}
public IEnumerable<KeyValuePair<string, string>> Headers
{
get
{
return this._response.Headers.AllKeys.Select(headerName => new KeyValuePair<string,
string>(headerName, _response.Headers.Get(headerName)));
}
}
public string GetHeader(string headerName)
{
if (headerName == null)
throw new ArgumentNullException("headerName");
return this._response.Headers.Get(headerName);
}
public int StatusCode { get; set; }
public Uri Url { get; set; }
public string Method { get; set; }
public void SetHeader(string headerName, string headerValue)
{
headers[headerName] = headerValue;
}
public Stream GetStream()
{
return this._response.GetResponseStream();
}
}
class Program
{
static void Main(string[] args)
{
HttpWebRequest request = WebRequest.CreateHttp("http://services.odata.org/V4/OData/OData.svc/$metadata");
ODataResponseMessage metadataMessage = new ODataResponseMessage(request.GetResponse());
ODataMessageReaderSettings readerSettings = new ODataMessageReaderSettings();
readerSettings.MessageQuotas.MaxReceivedMessageSize = int.MaxValue;
readerSettings.Validations = ValidationKinds.None;
IEdmModel model;
using (ODataMessageReader messageReader = new ODataMessageReader(metadataMessage, readerSettings))
{
model = messageReader.ReadMetadataDocument();
}
}
}
}
我得到的例外是:
Microsoft.OData.ODataException 未处理 HResult=-2146233079
Message=无法从消息内容中读取元数据文档。UnexpectedXmlAttribute :在给定的上下文中不需要属性“ConcurrencyMode”。: (1, 2043)