Catch up with .NET Core — System.IO.Pipelines

TL;DR

Introduction to System.IO.Pipelines. See Reference section for stuff you might find useful to delve in.

System.IO.Pipelines is a library that is designed to make it easier to do high-performance IO shipped in .NET Standard 2.1 as a first class BCL API.

Provides programming model to perform high-performance parsing of streaming data. Developers are free from having to manage buffers. A Pipeline is like a Stream that pushes data to you. Networking is the primary use case for this API, the whole thing emerged from Kestrel as an implementation detail.

There are no explicit buffers allocated. All buffer management is delegated to the and implementations. Delegating buffer management makes it easier for consuming code to focus solely on business logic.

Example from docs.microsoft: https://docs.microsoft.com/en-us/dotnet/standard/io/pipelines#pipe-basic-usage

Example: reading of buffered integers

On the high level, it looks something like the snippet below. FillPipeAsync is data source that uses writer object to manage buffer.

There are no explicit buffers allocated. All buffer management is delegated to the PipeReader and PipeWriter implementations. Delegating buffer management makes it easier for consuming code to focus solely on the business logic.

Output:

See full example at GitHub: blog-examples/pipelines

Streams and pipes

was designed to make writing of high performance parsing of streaming data. The vast majority of API working with IO are written based on . To simplify this, BCL provides out of the box ways to converts between these types.

Stream → Pipe

When working with a stream it is possible to create wrappers/adapters around Stream. Use PipeWriter.Create / PipeReader.Create

Example: https://github.com/davidfowl/TcpEcho

Pipe → Stream

When reading or writing stream data, you typically read data using a de-serializer and write data using a serializer. Most of these read and write stream APIs have a Stream parameter. To make it easier and expose an AsStream. AsStream returns a Stream implementation around the PipeReader or PipeWriter.

Originally published at https://nikiforovall.github.io.

Software Engineer at EPAM