Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **Socket Class (Primary Reference)**
- | Resource | URL |
- |----------|-----|
- | Socket API Reference | https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket?view=net-10.0 |
- | Socket Overview | https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-net-sockets-socket |
- | ConnectAsync | https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.connectasync?view=net-9.0 |
- | SendAsync | https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.sendasync?view=net-8.0 |
- | ReceiveAsync | https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receiveasync?view=net-8.0 |
- | Complete Socket Services Tutorial | https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/sockets/socket-services |
- **TcpClient/TcpListener (Higher-Level APIs)**
- | Resource | URL |
- |----------|-----|
- | TcpClient API | https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient?view=net-9.0 |
- | TcpListener API | https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener?view=net-10.0 |
- | TCP Classes Tutorial | https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/sockets/tcp-classes |
- | AcceptTcpClientAsync | https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener.accepttcpclientasync?view=net-9.0 |
- | NetworkStream API | https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.networkstream?view=net-10.0 |
- **Critical .NET 8 performance improvements** are documented in Stephen Toub's annual deep-dive at https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/ — a **200+ page** analysis covering JIT optimizations, async improvements, and networking enhancements. The networking-specific improvements are covered at https://devblogs.microsoft.com/dotnet/dotnet-8-networking-improvements/.
- ## ValueTask versus Task for networking code
- For high-performance networking, understanding when to use `ValueTask` is essential. Socket methods like `ReceiveAsync` now return `ValueTask<int>` for `Memory<byte>` overloads [Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receiveasync?view=net-8.0) because network operations often complete synchronously when data is already buffered.
- | Resource | URL |
- |----------|-----|
- | ValueTask<T> API | https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.valuetask-1?view=net-9.0 |
- | CA2012: Use ValueTasks Correctly | https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2012 |
- | Understanding Task vs ValueTask (Video) | https://learn.microsoft.com/en-us/shows/on-dotnet/understanding-how-to-use-task-and-valuetask |
- | ConfigureAwait FAQ | https://devblogs.microsoft.com/dotnet/configureawait-faq/ |
- **Key constraints**: ValueTask can only be awaited once, cannot be stored, and should not be used with combinators like `Task.WhenAll`. The official guidance states that `Task` remains the default choice — only use `ValueTask` when performance analysis proves worthwhile.
- ## System.Threading.Channels for producer-consumer patterns
- Channels provide the ideal abstraction for networking code that needs to decouple message production from consumption. They handle backpressure elegantly and are optimized for high-throughput scenarios.
- | Resource | URL |
- |----------|-----|
- | **Channels Conceptual Guide** | https://learn.microsoft.com/en-us/dotnet/core/extensions/channels |
- | Channel Factory Methods | https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels.channel?view=net-8.0 |
- | BoundedChannelOptions | https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels.boundedchanneloptions?view=net-8.0 |
- | ChannelReader<T> | https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels.channelreader-1?view=net-8.0 |
- | ChannelWriter<T> | https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels.channelwriter-1?view=net-8.0 |
- | **Stephen Toub's Introduction** | https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels/ |
- Stephen Toub's blog post is the definitive resource, explaining `SingleReader`/`SingleWriter` optimizations that eliminate locking overhead when only one consumer or producer exists [Microsoft](https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels/) — critical for networking scenarios where a single receive loop processes incoming data.
- ## Concurrency primitives for flow control
- | Resource | URL |
- |----------|-----|
- | SemaphoreSlim (async locking) | https://learn.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim?view=net-8.0 |
- | SemaphoreSlim.WaitAsync | https://learn.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim.waitasync?view=net-9.0 |
- | Semaphore Conceptual Guide | https://learn.microsoft.com/en-us/dotnet/standard/threading/semaphore-and-semaphoreslim |
- | ConcurrentDictionary<K,V> | https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2?view=net-8.0 |
- | GetOrAdd Method | https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd?view=net-8.0 |
- | **David Fowler's Async Guidance** | https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md |
- David Fowler's GitHub guidance document is **essential reading** — it covers async locking with `SemaphoreSlim`, common ConcurrentDictionary pitfalls, and patterns that the ASP.NET Core team uses in production.
- ## Binary serialization with BinaryPrimitives and Span<T>
- For protocol implementations requiring explicit endianness control, `BinaryPrimitives` provides zero-allocation reads and writes directly to spans — far superior to the older `BitConverter` class.
- | Resource | URL |
- |----------|-----|
- | **BinaryPrimitives Class** | https://learn.microsoft.com/en-us/dotnet/api/system.buffers.binary.binaryprimitives?view=net-8.0 |
- | ReadInt32BigEndian | https://learn.microsoft.com/en-us/dotnet/api/system.buffers.binary.binaryprimitives.readint32bigendian?view=net-8.0 |
- | WriteInt32BigEndian | https://learn.microsoft.com/en-us/dotnet/api/system.buffers.binary.binaryprimitives.writeint32bigendian?view=net-8.0 |
- | Span<T> API | https://learn.microsoft.com/en-us/dotnet/api/system.span-1?view=net-8.0 |
- | Memory<T> API | https://learn.microsoft.com/en-us/dotnet/api/system.memory-1?view=net-8.0 |
- | **Memory/Span Usage Guidelines** | https://learn.microsoft.com/en-us/dotnet/standard/memory-and-spans/memory-t-usage-guidelines |
- | ArrayPool<T> | https://learn.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1?view=net-8.0 |
- | SequenceReader<T> | https://learn.microsoft.com/en-us/dotnet/api/system.buffers.sequencereader-1?view=net-8.0 |
- **Stephen Toub's "All About Span"** [Microsoft Learn](https://learn.microsoft.com/en-us/archive/msdn-magazine/authors/stephen_toub) [Microsoft Docs](https://docs.microsoft.com/de-de/archive/msdn-magazine/authors/stephen_toub) at https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp-all-about-span-exploring-a-new-net-mainstay remains the foundational article explaining Span's design, stack-only constraints, and performance characteristics. [Microsoft Learn](https://learn.microsoft.com/en-us/archive/msdn-magazine/authors/stephen_toub)
- ## System.IO.Pipelines for high-performance I/O
- Pipelines represent the modern approach to high-performance networking in .NET, providing built-in backpressure, buffer pooling, and efficient handling of incomplete reads. [Microsoft](https://devblogs.microsoft.com/dotnet/author/davifowl/)
- | Resource | URL |
- |----------|-----|
- | **Pipelines Guide** | https://learn.microsoft.com/en-us/dotnet/standard/io/pipelines |
- | Pipe Class | https://learn.microsoft.com/en-us/dotnet/api/system.io.pipelines.pipe?view=net-8.0 |
- | PipeReader | https://learn.microsoft.com/en-us/dotnet/api/system.io.pipelines.pipereader?view=net-8.0 |
- | PipeWriter | https://learn.microsoft.com/en-us/dotnet/api/system.io.pipelines.pipewriter?view=net-8.0 |
- | Work with Buffers | https://learn.microsoft.com/en-us/dotnet/standard/io/buffers |
- | **David Fowler's Pipelines Blog** | https://devblogs.microsoft.com/dotnet/system-io-pipelines-high-performance-io-in-net/ |
- ## Protocol design RFCs and standards
- Understanding reliable transport protocols requires studying the original specifications. **RFC 9293** is the modern consolidated TCP specification that obsoletes the original RFC 793.
- **TCP and Reliable Delivery**
- | RFC | URL | Key Content |
- |-----|-----|-------------|
- | **RFC 9293 (TCP, 2022)** | https://www.rfc-editor.org/rfc/rfc9293.html | Sequence numbers, ACKs, state machine, flow control |
- | RFC 793 (Original TCP) | https://www.rfc-editor.org/rfc/rfc793.html | Foundational design philosophy, state diagrams |
- | **RFC 6298 (RTO Computation)** | https://www.rfc-editor.org/rfc/rfc6298.html | Retransmission timer, exponential backoff algorithm |
- | RFC 5681 (Congestion Control) | https://www.rfc-editor.org/rfc/rfc5681.html | Slow start, fast retransmit, duplicate ACK handling |
- **Selective Acknowledgments**
- | RFC | URL | Key Content |
- |-----|-----|-------------|
- | **RFC 2018 (TCP SACK)** | https://www.rfc-editor.org/rfc/rfc2018.html | SACK option format, block ranges |
- | RFC 6675 (SACK Loss Recovery) | https://www.rfc-editor.org/rfc/rfc6675.html | Scoreboard data structure, conservative recovery |
- | **RFC 4960 (SCTP)** | https://www.rfc-editor.org/rfc/rfc4960.html | Cleaner SACK model, TSN-based acknowledgment |
- **Modern Protocol Patterns (QUIC)**
- | RFC | URL | Key Content |
- |-----|-----|-------------|
- | **RFC 9000 (QUIC Transport)** | https://www.rfc-editor.org/rfc/rfc9000.html | Streams, flow control, framing, connection migration |
- | **RFC 9002 (QUIC Loss Detection)** | https://www.rfc-editor.org/rfc/rfc9002.pdf | PTO mechanism, modern exponential backoff |
- | RFC 9292 (Binary HTTP) | https://www.rfc-editor.org/rfc/rfc9292.html | Length-prefixed framing example |
- **RFC 9002** is particularly valuable for modern implementations — it describes QUIC's probe timeout mechanism, which is more sophisticated than TCP's RTO computation.
- ## Length-prefixed framing and message boundaries
- TCP provides a byte stream without message boundaries. [CodeProject](https://www.codeproject.com/Articles/37496/TCP-IP-Protocol-Design-Message-Framing) Stephen Cleary's articles explain why framing is necessary and how to implement it:
- | Resource | URL |
- |----------|-----|
- | **Message Framing Explanation** | https://blog.stephencleary.com/2009/04/message-framing.html |
- | Length-Prefix Implementation | https://blog.stephencleary.com/2009/04/sample-code-length-prefix-message.html |
- ## State machine patterns in C#
- | Resource | URL |
- |----------|-----|
- | Records Reference | https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record |
- | Record Structs (C# 10) | https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/record-structs |
- | **Enum Design Guidelines** | https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/enum |
- | Pattern Matching | https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns |
- | Switch Expressions | https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression |
- | Enumeration Classes Pattern | https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types |
- ## Real-world reference: Kestrel socket transport
- ASP.NET Core's Kestrel web server provides production-grade socket handling code that demonstrates these patterns in practice:
- | File | URL |
- |------|-----|
- | **SocketConnection.cs** | https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs |
- | SocketConnectionListener.cs | https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Transport.Sockets/src/SocketConnectionListener.cs |
- | SocketTransportOptions.cs | https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Transport.Sockets/src/SocketTransportOptions.cs |
- | Transport.Sockets Directory | https://github.com/dotnet/aspnetcore/tree/main/src/Servers/Kestrel/Transport.Sockets/src |
- `SocketConnection.cs` is the **primary reference** — it demonstrates duplex pipe setup, receive/send loops, shutdown handling, and proper connection lifecycle management.
- ## Testing async networking code
- **.NET 8 introduced TimeProvider** — an official abstraction for time [Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/standard/datetime/timeprovider-overview) that replaces custom `ISystemClock` implementations and enables deterministic testing of timeout logic.
- | Resource | URL |
- |----------|-----|
- | **TimeProvider Overview** | https://learn.microsoft.com/en-us/dotnet/standard/datetime/timeprovider-overview |
- | TimeProvider API | https://learn.microsoft.com/en-us/dotnet/api/system.timeprovider?view=net-8.0 |
- | FakeTimeProvider | https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.time.testing.faketimeprovider?view=net-9.0-pp |
- | NuGet Package | https://www.nuget.org/packages/Microsoft.Extensions.TimeProvider.Testing/ |
- **Testing Frameworks**
- | Resource | URL |
- |----------|-----|
- | NUnit Async Tests | https://docs.nunit.org/articles/nunit/writing-tests/attributes/test.html |
- | xUnit Shared Context | https://xunit.net/docs/shared-context |
- | MSTest Lifecycle | https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-writing-tests-lifecycle |
- | **Async Unit Testing Guide** | https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/november/async-programming-unit-testing-asynchronous-code |
- **Chaos Engineering with Polly**
- | Resource | URL |
- |----------|-----|
- | Polly Documentation | https://www.pollydocs.org/ |
- | **Chaos Engineering Blog** | https://devblogs.microsoft.com/dotnet/resilience-and-chaos-engineering/ |
- | Polly GitHub | https://github.com/App-vNext/Polly |
- Polly v8.3+ integrates Simmy for fault injection, enabling injection of exceptions, artificial latency, and fake results to test resilience.
- ## Essential async and disposal patterns
- | Resource | URL |
- |----------|-----|
- | **IAsyncDisposable Implementation** | https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync |
- | IAsyncDisposable API | https://learn.microsoft.com/en-us/dotnet/api/system.iasyncdisposable?view=net-9.0 |
- | **Cancellation Overview** | https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads |
- | Linked CancellationTokens | https://learn.microsoft.com/en-us/dotnet/standard/threading/how-to-listen-for-multiple-cancellation-requests |
- | CancellationToken Patterns | https://devblogs.microsoft.com/premier-developer/recommended-patterns-for-cancellationtoken/ |
- | **Async Best Practices** | https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming |
- | TPL Exception Handling | https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/exception-handling-task-parallel-library |
Advertisement
Add Comment
Please, Sign In to add comment