/// <summary> /// Uploads the given file. /// </summary> /// <param name="fileContent">Represents the content of the file</param> /// <returns>The <see cref="FileReference"/> instance that represents the reference to created file.</returns> public async Task <FileReference> UploadFileAsync(FileContentSource fileContent) { if (fileContent == null) { throw new ArgumentNullException(nameof(fileContent)); } var stream = fileContent.OpenReadStream(); try { if (stream.Length > MAX_FILE_SIZE_MB * 1024 * 1024) { throw new ArgumentException($"Maximum supported file size is {MAX_FILE_SIZE_MB} MB.", nameof(stream)); } var endpointUrl = _urlBuilder.BuildUploadFileUrl(fileContent.FileName); var response = await _actionInvoker.UploadFileAsync <FileReference>(endpointUrl, stream, fileContent.ContentType); return(response); } finally { // Dispose the stream only in case new stream was created if (fileContent.CreatesNewStream) { stream.Dispose(); } } }