Exemplo n.º 1
0
        /// <summary>
        /// Reads the DataServiceVersion header from the <paramref name="message"/> and parses it.
        /// If no DataServiceVersion header is found it sets the default version to be used for reading.
        /// </summary>
        /// <param name="message">The message to get the data service version header from.</param>
        /// <param name="defaultVersion">The default version to use if the header was not specified.</param>
        /// <returns>
        /// The <see cref="ODataVersion"/> retrieved from the DataServiceVersion header of the message.
        /// The default version if none is specified in the header.
        /// </returns>
        public static ODataVersion GetDataServiceVersion(this IODataRequestMessage message, ODataVersion defaultVersion)
        {
            // translation from IODataResponseMessage to ODataMessage to pass into GetDataServiceVersion.
            // we retain all of the data we need from the message, with a few extra data that aren't used in.
            // GetDataServiceVersion. Not ideal, but it works.
            ODataMessage odataMessage = new ODataRequestMessage(message, false /*writing*/, false /*disableMessageStreamDisposal*/, Int64.MaxValue /*maxMessageSize*/);

            return(ODataUtilsInternal.GetDataServiceVersion(odataMessage, defaultVersion));
        }
#pragma warning disable 1998
        public override async Task StartBatchAsync()
        {
            _requestMessage = new ODataRequestMessage() { Url = _session.Settings.BaseUri };
            _messageWriter = new ODataMessageWriter(_requestMessage);
#if SILVERLIGHT
            _batchWriter = _messageWriter.CreateODataBatchWriter();
            _batchWriter.WriteStartBatch();
#else
            _batchWriter = await _messageWriter.CreateODataBatchWriterAsync();
            await _batchWriter.WriteStartBatchAsync();
#endif
        }
#pragma warning disable 1998
        protected override async Task<Stream> WriteLinkContentAsync(string linkIdent)
        {
            var message = new ODataRequestMessage();
            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var link = new ODataEntityReferenceLink
                {
                    Url = Utils.CreateAbsoluteUri(_session.Settings.BaseUri.AbsoluteUri, linkIdent)
                };
                messageWriter.WriteEntityReferenceLink(link);

#if SILVERLIGHT
                return message.GetStream();
#else
                return await message.GetStreamAsync();
#endif
            }
        }
        protected override async Task<Stream> WriteStreamContentAsync(Stream stream)
        {
            var message = new ODataRequestMessage();
            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(true), _model))
            {
#if SILVERLIGHT
                messageWriter.WriteValue(Utils.StreamToString(stream));
#else
                await messageWriter.WriteValueAsync(Utils.StreamToString(stream));
#endif
                return await message.GetStreamAsync();
            }
        }
#pragma warning restore 1998

        protected override async Task<Stream> WriteActionContentAsync(string actionName, IDictionary<string, object> parameters)
        {
            var message = new ODataRequestMessage();
            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var action = _model.SchemaElements
                    .Where(x => x.SchemaElementKind == EdmSchemaElementKind.EntityContainer)
                    .SelectMany(x => (x as IEdmEntityContainer).FunctionImports())
                    .BestMatch(x => x.Name, actionName, _session.Pluralizer);
                var parameterWriter = messageWriter.CreateODataParameterWriter(action);

                parameterWriter.WriteStart();

                foreach (var parameter in parameters)
                {
                    if (!(parameter.Value is string) && parameter.Value is IEnumerable)
                    {
                        var collectionWriter = parameterWriter.CreateCollectionWriter(parameter.Key);
                        collectionWriter.WriteStart(new ODataCollectionStart());
                        foreach (var item in parameter.Value as IEnumerable)
                        {
                            collectionWriter.WriteItem(item);
                        }
                        collectionWriter.WriteEnd();
                    }
                    else
                    {
                        parameterWriter.WriteValue(parameter.Key, parameter.Value);
                    }
                }

                parameterWriter.WriteEnd();
                return await message.GetStreamAsync();
            }
        }
        protected override async Task<Stream> WriteStreamContentAsync(Stream stream, bool writeAsText)
        {
            var message = new ODataRequestMessage();
            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(true), _model))
            {
                var value = writeAsText ? (object)Utils.StreamToString(stream) : Utils.StreamToByteArray(stream);
#if SILVERLIGHT
                messageWriter.WriteValue(value);
#else
                await messageWriter.WriteValueAsync(value);
#endif
                return await message.GetStreamAsync();
            }
        }
        protected override async Task<Stream> WriteActionContentAsync(string actionName, IDictionary<string, object> parameters)
        {
            var message = new ODataRequestMessage();
            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var action = _model.SchemaElements
                    .Where(x => x.SchemaElementKind == EdmSchemaElementKind.EntityContainer)
                    .SelectMany(x => (x as IEdmEntityContainer).FunctionImports())
                    .BestMatch(x => x.Name, actionName, _session.Pluralizer);
                var parameterWriter = messageWriter.CreateODataParameterWriter(action);

                parameterWriter.WriteStart();

                foreach (var parameter in parameters)
                {
                    var actionParameter = action.Parameters.BestMatch(x => x.Name, parameter.Key, _session.Pluralizer);
                    if (actionParameter == null)
                        throw new UnresolvableObjectException(parameter.Key, string.Format("Parameter [{0}] not found for action [{1}]", parameter.Key, actionName));

                    if (actionParameter.Type.Definition.TypeKind == EdmTypeKind.Collection)
                    {
                        var collectionWriter = parameterWriter.CreateCollectionWriter(parameter.Key);
                        collectionWriter.WriteStart(new ODataCollectionStart());
                        foreach (var item in parameter.Value as IEnumerable)
                        {
                            collectionWriter.WriteItem(item);
                        }
                        collectionWriter.WriteEnd();
                    }
                    else
                    {
                        parameterWriter.WriteValue(parameter.Key, parameter.Value);
                    }
                }

                parameterWriter.WriteEnd();
                return await message.GetStreamAsync();
            }
        }