/// <summary>
        ///     Creates a new instance of the <see cref="ProtoBufContent" /> class that will contain the
        ///     <see cref="value" />  serialized as ProtoBuf.
        /// </summary>
        /// <param name="type">The type of the value to serialize.</param>
        /// <param name="value">The value to serialize.</param>
        /// <param name="typeModel">Options to control the behavior during serialization.</param>
        /// <param name="mediaType">The media type to use for the content.</param>
        /// <returns>A <see cref="ProtoBufContent" /> instance.</returns>
        public static ProtoBufContent Create(Type type,
                                             object?value, TypeModel?typeModel = null, MediaTypeHeaderValue?mediaType = null)
        {
            var formatter = new ProtoBufMediaTypeFormatter(typeModel);

            return(new ProtoBufContent(type, value, formatter, mediaType));
        }
Beispiel #2
0
        public void DefaultConstructor()
        {
            // Act
            var formatter = new ProtoBufMediaTypeFormatter();

            // Assert
            Assert.NotNull(formatter.TypeModel);
        }
Beispiel #3
0
        public void ConstructorWithModel()
        {
            // Arrange
            var model = RuntimeTypeModel.Create();

            // Act
            var formatter = new ProtoBufMediaTypeFormatter(model);

            // Assert
            Assert.Same(model, formatter.TypeModel);
        }
Beispiel #4
0
        /// <summary>
        ///     Reads the HTTP content and returns the value that results from deserializing the content as ProtoBuf in an
        ///     asynchronous operation.
        /// </summary>
        /// <param name="content">The content to read from.</param>
        /// <param name="type">The type of the object to deserialize to and return.</param>
        /// <param name="typeModel">Options to control the behavior during deserialization.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of
        ///     cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public static async Task <object?> ReadFromProtoBufAsync(this HttpContent content, Type type,
                                                                 TypeModel?typeModel = null, CancellationToken cancellationToken = default)
        {
            Guard.NotNull(type, nameof(type));
            Guard.NotNull(content, nameof(content));

            if (content is ObjectContent objectContent)
            {
                return(objectContent.Value);
            }

            var formatter = new ProtoBufMediaTypeFormatter(typeModel);

            using var readStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

            return(await formatter.ReadFromStreamAsync(type, readStream, content, null, cancellationToken)
                   .ConfigureAwait(false));
        }
Beispiel #5
0
 public ProtoBufMediaTypeFormatterTest()
 {
     _cancellationToken = new CancellationToken();
     _formatter         = new ProtoBufMediaTypeFormatter();
     _content           = new ProtoBufHttpContent(_formatter.TypeModel);
 }
 public FunctionalTest()
 {
     _formatter = new ProtoBufMediaTypeFormatter();
 }
 public MediaTypeFormatterTest()
 {
     _formatter = new ProtoBufMediaTypeFormatter();
 }
 private ProtoBufContent(Type type, object?value, ProtoBufMediaTypeFormatter formatter,
                         MediaTypeHeaderValue?mediaType)
     : base(type, value, formatter, mediaType)
 {
     TypeModel = formatter.TypeModel;
 }
Beispiel #9
0
 public ProtoBufMediaTypeFormatterTest()
 {
     _formatter = new ProtoBufMediaTypeFormatter();
     _content   = new ProtoBufHttpContent(_formatter.Model);
 }