3

I am trying to use a sample v4 OData service http://services.odata.org/V4/OData/OData.svc/ with the new OData v7. When I call ODataMessageReader.ReadMetadataDocument I am getting a UnexpectedXmlAttribute exception. Is it possible to ignore the unsupported attributes and elements?

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();
            }
        }
    }
}

The exception I am getting is:

Microsoft.OData.ODataException was unhandled HResult=-2146233079
Message=The metadata document could not be read from the message content. UnexpectedXmlAttribute : The attribute 'ConcurrencyMode' was not expected in the given context. : (1, 2043)

RobSiklos
  • 8,348
  • 5
  • 47
  • 77
Andrey Belykh
  • 2,578
  • 4
  • 32
  • 46
  • The only thing I could find is this one: https://groups.google.com/forum/#!topic/odata-discussion/zzKiUtG6C6k, which kind of explains why exception is occurring. However, I need to just ignore and continue, not ask customers to rebuild their project, – Andrey Belykh Jun 02 '17 at 14:38
  • Cross ref https://github.com/OData/odata.net/issues/822 – Andrey Belykh Jun 13 '17 at 13:47

0 Answers0