Imported from rabbitmq/rabbitmq-amqp-dotnet-client (
AGENTS.md). Install upstream withnpx skills add rabbitmq/rabbitmq-amqp-dotnet-client. Copyright stays with the author.
Agent Guide
This document provides essential information for AI agents working on the RabbitMQ AMQP 1.0 .NET Client codebase.
Project Overview
This is a .NET client library for RabbitMQ that implements the AMQP 1.0 protocol. The library is designed to work with RabbitMQ 4.x and provides a high-level API for connecting to RabbitMQ, publishing messages, consuming messages, and managing RabbitMQ entities.
Current version: 0.51.0
Key Directories
RabbitMQ.AMQP.Client/
The main library code:
- Public interfaces:
IEnvironment,IConnection,IPublisher,IConsumer,IRequester,IResponder,IManagement,ILifeCycle,IRecoveryConfiguration,IBackOffDelayPolicy,ITopologyListener,IMetricsReporter, etc. - Implementation classes: Located in
Impl/subdirectory (e.g.,AmqpConnection,AmqpConsumer,AmqpPublisher,AmqpEnvironment) - Core abstractions: Interfaces define the public API, implementations are in
Impl/ - Other notable files:
Affinity.cs,ConnectionSettings.cs,SaslMechanism.cs,ByteCapacity.cs,Extensions.cs,Utils.cs,FeatureFlags.cs,Consts.cs
RabbitMQ.AMQP.Client/Impl/
All implementation classes:
AbstractLifeCycle.cs— Base lifecycle class; also containsAbstractReconnectLifeCyclewith back-off reconnect logicAmqpEnvironment.cs—IEnvironmentimplementation; manages a pool of connectionsAmqpConnection.cs— Main connection implementationAmqpConsumer.cs/AmqpConsumerBuilder.cs— Consumer and its builderAmqpPublisher.cs/AmqpPublisherBuilder.cs— Publisher and its builderAmqpManagement.cs/AmqpManagementParameters.cs— Management APIAmqpRequester.cs/AmqpResponder.cs— RPC-style request/responseAmqpSessionManagement.cs— AMQP session handlingAmqpMessage.cs— Message implementationAmqpQueueSpecification.cs,AmqpExchangeSpecification.cs,AmqpBindingSpecification.cs— Entity specsRecordingTopologyListener.cs— Records topology for recoveryUnsettledMessageCounter.cs— Tracks unsettled messagesAddressBuilder.cs,Visitor.cs,DeliveryContext.cs,BindingSpec.cs,QueueSpec.cs,ExchangeSpec.cs
Tests/
Test suite using xUnit:
- Top-level tests:
AmqpTests.cs,AnonymousPublisherTests.cs,BindingsTests.cs,ByteCapacityTests.cs,ClusterTests.cs,ConnectionRecoveryTests.cs,EnvironmentTests.cs,MessagesTests.cs,MetricsTests.cs,OAuth2Tests.cs,TlsConnectionTests.cs,UtilsTests.cs,AddressBuilderTests.cs - Subdirectories by feature area:
Affinity/— Node affinity testsAmqp091/— AMQP 0-9-1 compatibility testsConnectionTests/—ConnectionTests.cs,ConnectionSettingsTests.cs,SaslConnectionTests.csConsumer/—BasicConsumerTests.cs,ConsumerDispositionTests.cs,ConsumerOutcomeTests.cs,ConsumerPauseTests.cs,ConsumerSqlFilterTests.cs,PreSettledConsumerTests.cs,StreamConsumerTests.csDirectReply/— Direct reply-to testsManagement/—ManagementTests.cs,MockManagementTests.csPublisher/—PublisherTests.csRecovery/—PublisherConsumerRecoveryTests.cs,CustomPublisherConsumerRecoveryTests.csRequesterResponser/— RPC testsSessions/— Session management tests
IntegrationTest.cs/IntegrationTest.Static.cs— Common test infrastructureHttpApiClient.cs— HTTP management API client used in tests
docs/Examples/
Example code demonstrating library usage:
GettingStartedAffinityBatchDispositionsConsumerTimeout(quorum queuex-consumer-timeout)HAClientOAuth2OpenTelemetryIntegrationPerformanceTestPresettledRpc(Requester/Responder)StreamFilterWebSockets
Architecture Patterns
Interface-Based Design
- Public API is defined through interfaces (e.g.,
IConnection,IPublisher,IConsumer) - Implementations are in the
Impl/namespace AmqpEnvironmentis the only public entry point; useAmqpEnvironment.Create(connectionSettings)to bootstrap
Builder Pattern
Builders are used for constructing complex objects:
IPublisherBuilder→IPublisherIConsumerBuilder→IConsumerIRequesterBuilder→IRequesterIResponderBuilder→IResponderConnectionSettingsBuilder→ConnectionSettings
Lifecycle Management
- Most entities implement
ILifeCycle(extendsIDisposable) - States:
Open,Reconnecting,Closing,Closed - Entities expose
CloseAsync()and aChangeStateevent (LifeCycleCallBackdelegate) AbstractLifeCycleis the base class;AbstractReconnectLifeCycleadds back-off reconnect logicAmqpNotOpenExceptionis thrown when an operation is attempted on a non-open resource
Recovery / Reconnection
- Configured via
IRecoveryConfiguration/RecoveryConfiguration Activated(bool)— enable/disable reconnect (default: enabled)Topology(bool)— enable/disable topology recovery after reconnect (default: disabled)BackOffDelayPolicy(IBackOffDelayPolicy)— customise delay between reconnect attemptsRecordingTopologyListenerrecords declared entities for topology recovery
Node Affinity
IAffinity/DefaultAffinityinAffinity.cs— connect to the node that owns a specific queueOperation.Publishtargets the queue leader;Operation.Consumetargets a followerAffinityUtils.TryToFindUriNodeiterates connections until the correct node is found- Configured via
ConnectionSettings.Affinity
Consumer Settle Strategies (ConsumerSettleStrategy)
ExplicitSettle— default; messages must be settled manually viaIContextPreSettled— messages are auto-settled on receipt (no redelivery on failure)DirectReplyTo— enables direct reply-to consumer (pre-settled by default)- Set via
IConsumerBuilder.SettleStrategy(ConsumerSettleStrategy...)
Async/Await Pattern
- All I/O operations are asynchronous
- Methods return
TaskorTask<T> - Use
async/awaitthroughout the codebase
Code Conventions
Naming
- Interfaces start with
I(e.g.,IConnection,IPublisher) - Implementation classes are prefixed with
Amqp(e.g.,AmqpConnection,AmqpPublisher) - Private fields use
_camelCase - Public properties use
PascalCase
File Organization
- One public interface/class per file
- Implementation classes in
Impl/subdirectory - File names match class/interface names
Error Handling
- Custom exceptions:
ConnectionException,ManagementException,PublisherException,ConsumerException AmqpNotOpenException— thrown when operating on a closed/closing/reconnecting resourceInternalBugException— for internal errors that should never occur
Thread Safety
IEnvironmentinstances are expected to be thread-safeAmqpEnvironmentusesConcurrentDictionaryfor connection tracking andInterlockedfor IDs- Connection instances should handle concurrent operations safely
Important Files
Build Configuration
Directory.Build.props— Common build properties; treats warnings as errors (TreatWarningsAsErrors=true)Directory.Packages.props— Centralized package version managementglobal.json— .NET SDK version specificationBuild.csproj— Main build/test projectMakefile— Convenience targets
Documentation
CHANGELOG.md— Release notes and changesREADME.md— Project overview and quick startPublicAPI.Shipped.txt/PublicAPI.Unshipped.txt— API surface tracking
Key Implementation Files
Impl/AmqpEnvironment.cs— Entry point; manages connectionsImpl/AmqpConnection.cs— Main connection implementationImpl/AmqpConsumer.cs— Consumer implementationImpl/AmqpPublisher.cs— Publisher implementationImpl/AmqpManagement.cs— Management API implementationImpl/AbstractLifeCycle.cs— Base lifecycle and reconnect logicImpl/RecordingTopologyListener.cs— Topology recovery supportConnectionSettings.cs— Connection configurationAffinity.cs— Node affinity logicIRecoveryConfiguration.cs— Recovery configuration interface and default implementationFeatureFlags.cs— Feature flag definitions
Testing
Running Tests
make test
Test Infrastructure
- Tests require a running RabbitMQ instance
- Use
make rabbitmq-cluster-startto start cluster broker - Use
make rabbitmq-server-startto start single node IntegrationTest.csprovides common test utilitiesHttpApiClient.cswraps the RabbitMQ HTTP management API for test assertions
Test Organization
- Tests are organized by feature area (Consumer, Publisher, Management, Recovery, etc.)
- Integration tests verify end-to-end functionality
- Some tests use mocks (e.g.,
MockManagementTests.cs)
Development Workflow
Building
make
Build configuration is managed through Directory.Build.props.
Code Quality
- Warnings are treated as errors (
TreatWarningsAsErrorsistrue) - Public API changes must be tracked in
PublicAPI.Unshipped.txt
Versioning
- Version information is managed in project files
- Releases are tagged in git (e.g.,
v0.51.0) - Changelog entries follow a specific format (see
CHANGELOG.md)
Common Tasks
Adding a New Feature
- Define public interface(s) in
RabbitMQ.AMQP.Client/ - Implement in
RabbitMQ.AMQP.Client/Impl/ - Add tests in
Tests/ - Update
CHANGELOG.md - Update
PublicAPI.Unshipped.txtif adding public APIs - Add example in
docs/Examples/if applicable
Modifying Existing Features
- Check if changes affect public API (update
PublicAPI.Unshipped.txtif so) - Update implementation in
Impl/ - Update/add tests
- Update
CHANGELOG.mdif user-facing
Fixing Bugs
- Add test case that reproduces the bug
- Fix the implementation
- Verify test passes
- Update
CHANGELOG.mdunder the appropriate section (Fix, Changed, etc.)
Dependencies
External Libraries
- Microsoft AMQP.Net Lite — Core AMQP protocol implementation
- xUnit — Testing framework
- Other dependencies managed in
Directory.Packages.props
Important Notes
- The library uses AMQP 1.0 protocol (not AMQP 0-9-1)
- Designed for RabbitMQ 4.x
- All operations are asynchronous
- Thread-safety is important for
IEnvironmentand connection instances - The library supports:
- WebSockets (added in v0.50.0)
- OAuth2 authentication
- TLS connections
- Direct reply-to
- Connection recovery with topology replay
- Streams (with offset, filter, and SQL filter expression support — SQL filters require RabbitMQ 4.2+)
- Node affinity (publish to leader, consume from follower)
- Pre-settled consumers (added in v0.51.0)
- OpenTelemetry metrics integration
- SASL authentication mechanisms