public async Task <uint> SetLOBBuffered(Stream value, DBTransaction transaction) { if (value.CanSeek) { value.Position = 0; } var count = 0; var bufferSize = 81920; var buffer = new byte[bufferSize]; var tempFileName = Helper.GetDocumentsFullPath(Path.GetRandomFileName(), "Temp"); try { using (var tempStream = new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite)) { while ((count = await value.ReadAsync(buffer, 0, bufferSize)) != 0) { tempStream.Write(buffer, 0, count); } } var manager = new NpgsqlLargeObjectManager((NpgsqlConnection)transaction.Connection); var result = await(Task <object>) manager.ImportRemoteAsync(tempFileName, 0, CancellationToken.None); return((uint)result); } finally { File.Delete(tempFileName); } }