Exemplo n.º 1
0
 internal Task CopyToAsync(
     Stream stream,
     TransportContext context,
     CancellationToken cancellationToken)
 {
     this.CheckDisposed();
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     try
     {
         ArraySegment <byte> buffer;
         if (this.TryGetBuffer(out buffer))
         {
             return(HttpContent.CopyToAsyncCore(stream.WriteAsync(new ReadOnlyMemory <byte>(buffer.Array, buffer.Offset, buffer.Count), cancellationToken)));
         }
         Task streamAsync = this.SerializeToStreamAsync(stream, context, cancellationToken);
         this.CheckTaskNotNull(streamAsync);
         return(HttpContent.CopyToAsyncCore(new ValueTask(streamAsync)));
     }
     catch (Exception ex) when(HttpContent.StreamCopyExceptionNeedsWrapping(ex))
     {
         return(Task.FromException(HttpContent.GetStreamCopyException(ex)));
     }
 }