Use C# client

This section introduces some hands-on examples to get started with using the Pulsar C# client.

Create a producer

This section describes how to create a producer.

  • Create a producer by using the builder.

    1. using DotPulsar;
    2. using DotPulsar.Extensions;
    3. var producer = client.NewProducer()
    4. .Topic("persistent://public/default/mytopic")
    5. .Create();
  • Create a producer without using the builder.

    1. using DotPulsar;
    2. var options = new ProducerOptions<byte[]>("persistent://public/default/mytopic", Schema.ByteArray);
    3. var producer = client.CreateProducer(options);

Monitor

This example shows how to monitor the producer’s state.

  1. private static async ValueTask Monitor(IProducer producer, CancellationToken cancellationToken)
  2. {
  3. var state = ProducerState.Disconnected;
  4. while (!cancellationToken.IsCancellationRequested)
  5. {
  6. state = (await producer.StateChangedFrom(state, cancellationToken)).ProducerState;
  7. var stateMessage = state switch
  8. {
  9. ProducerState.Connected => $"The producer is connected",
  10. ProducerState.Disconnected => $"The producer is disconnected",
  11. ProducerState.Closed => $"The producer has closed",
  12. ProducerState.Faulted => $"The producer has faulted",
  13. ProducerState.PartiallyConnected => $"The producer is partially connected.",
  14. _ => $"The producer has an unknown state '{state}'"
  15. };
  16. Console.WriteLine(stateMessage);
  17. if (producer.IsFinalState(state))
  18. return;
  19. }
  20. }

The following table lists states available for the producer.

StateDescription
ClosedThe producer or the Pulsar client has been disposed.
ConnectedAll is well.
DisconnectedThe connection is lost and attempts are being made to reconnect.
FaultedAn unrecoverable error has occurred.
PartiallyConnectedSome of the sub-producers are disconnected.

Create a consumer

This section describes how to create a consumer.

  • Create a consumer by using the builder.

    1. using DotPulsar;
    2. using DotPulsar.Extensions;
    3. var consumer = client.NewConsumer()
    4. .SubscriptionName("MySubscription")
    5. .Topic("persistent://public/default/mytopic")
    6. .Create();
  • Create a consumer without using the builder.

    1. using DotPulsar;
    2. var options = new ConsumerOptions<byte[]>("MySubscription", "persistent://public/default/mytopic", Schema.ByteArray);
    3. var consumer = client.CreateConsumer(options);

Monitor

This example shows how to monitor the consumer’s state.

  1. private static async ValueTask Monitor(IConsumer consumer, CancellationToken cancellationToken)
  2. {
  3. var state = ConsumerState.Disconnected;
  4. while (!cancellationToken.IsCancellationRequested)
  5. {
  6. state = (await consumer.StateChangedFrom(state, cancellationToken)).ConsumerState;
  7. var stateMessage = state switch
  8. {
  9. ConsumerState.Active => "The consumer is active",
  10. ConsumerState.Inactive => "The consumer is inactive",
  11. ConsumerState.Disconnected => "The consumer is disconnected",
  12. ConsumerState.Closed => "The consumer has closed",
  13. ConsumerState.ReachedEndOfTopic => "The consumer has reached end of topic",
  14. ConsumerState.Faulted => "The consumer has faulted",
  15. ConsumerState.Unsubscribed => "The consumer is unsubscribed.",
  16. _ => $"The consumer has an unknown state '{state}'"
  17. };
  18. Console.WriteLine(stateMessage);
  19. if (consumer.IsFinalState(state))
  20. return;
  21. }
  22. }

The following table lists states available for the consumer.

StateDescription
ActiveAll is well.
InactiveAll is well. The subscription type is Failover and you are not the active consumer.
ClosedThe consumer or the Pulsar client has been disposed.
DisconnectedThe connection is lost and attempts are being made to reconnect.
FaultedAn unrecoverable error has occurred.
ReachedEndOfTopicNo more messages are delivered.
UnsubscribedThe consumer has unsubscribed.

Create a reader

This section describes how to create a reader.

  • Create a reader by using the builder.

    1. using DotPulsar;
    2. using DotPulsar.Extensions;
    3. var reader = client.NewReader()
    4. .StartMessageId(MessageId.Earliest)
    5. .Topic("persistent://public/default/mytopic")
    6. .Create();
  • Create a reader without using the builder.

    1. using DotPulsar;
    2. var options = new ReaderOptions<byte[]>(MessageId.Earliest, "persistent://public/default/mytopic", Schema.ByteArray);
    3. var reader = client.CreateReader(options);

Monitor

This example shows how to monitor the reader’s state.

  1. private static async ValueTask Monitor(IReader reader, CancellationToken cancellationToken)
  2. {
  3. var state = ReaderState.Disconnected;
  4. while (!cancellationToken.IsCancellationRequested)
  5. {
  6. state = (await reader.StateChangedFrom(state, cancellationToken)).ReaderState;
  7. var stateMessage = state switch
  8. {
  9. ReaderState.Connected => "The reader is connected",
  10. ReaderState.Disconnected => "The reader is disconnected",
  11. ReaderState.Closed => "The reader has closed",
  12. ReaderState.ReachedEndOfTopic => "The reader has reached end of topic",
  13. ReaderState.Faulted => "The reader has faulted",
  14. _ => $"The reader has an unknown state '{state}'"
  15. };
  16. Console.WriteLine(stateMessage);
  17. if (reader.IsFinalState(state))
  18. return;
  19. }
  20. }

The following table lists states available for the reader.

StateDescription
ClosedThe reader or the Pulsar client has been disposed.
ConnectedAll is well.
DisconnectedThe connection is lost and attempts are being made to reconnect.
FaultedAn unrecoverable error has occurred.
ReachedEndOfTopicNo more messages are delivered.