Delen via


ServiceBusProcessor Class

Definition

The ServiceBusProcessor provides an abstraction around a set of ServiceBusReceiver that allows using an event based model for processing received ServiceBusReceivedMessage. It is constructed by calling CreateProcessor(String, ServiceBusProcessorOptions). The message handler is specified with the ProcessMessageAsync property. The error handler is specified with the ProcessErrorAsync property. To start processing after the handlers have been specified, call StartProcessingAsync(CancellationToken).

public class ServiceBusProcessor : IAsyncDisposable
type ServiceBusProcessor = class
    interface IAsyncDisposable
Public Class ServiceBusProcessor
Implements IAsyncDisposable
Inheritance
ServiceBusProcessor
Implements

Remarks

The ServiceBusProcessor is safe to cache and use for the lifetime of an application or until the ServiceBusClient that it was created by is disposed. Caching the processor is recommended when the application is processing messages regularly. The sender is responsible for ensuring efficient network, CPU, and memory use. Calling DisposeAsync() on the associated ServiceBusClient as the application is shutting down will ensure that network resources and other unmanaged objects used by the processor are properly cleaned up.

Constructors

ServiceBusProcessor()

Initializes a new instance of the ServiceBusProcessor class for mocking.

ServiceBusProcessor(ServiceBusClient, String, ServiceBusProcessorOptions)

Initializes a new instance of the ServiceBusProcessor class for use with derived types.

ServiceBusProcessor(ServiceBusClient, String, String, ServiceBusProcessorOptions)

Initializes a new instance of the ServiceBusProcessor class for use with derived types.

Properties

AutoCompleteMessages

Gets a value that indicates whether the processor should automatically complete messages after the message handler has completed processing. If the message handler triggers an exception, the message will not be automatically completed.

EntityPath

Gets the path of the Service Bus entity that the processor is connected to, specific to the Service Bus namespace that contains it.

FullyQualifiedNamespace

Gets the fully qualified Service Bus namespace that the receiver is associated with. This is likely to be similar to {yournamespace}.servicebus.windows.net.

Identifier

Gets the ID used to identify this processor. This can be used to correlate logs and exceptions.

IsClosed

Indicates whether or not this ServiceBusProcessor has been closed.

IsProcessing

Gets whether or not this processor is currently processing messages.

MaxAutoLockRenewalDuration

Gets the maximum duration within which the lock will be renewed automatically. This value should be greater than the longest message lock duration; for example, the LockDuration Property.

MaxConcurrentCalls

Gets the maximum number of concurrent calls to the ProcessMessageAsync message handler the processor should initiate.

PrefetchCount

Gets the number of messages that will be eagerly requested from Queues or Subscriptions during processing. This is intended to help maximize throughput by allowing the processor to receive from a local cache rather than waiting on a service request.

ReceiveMode

Gets the ReceiveMode used to specify how messages are received. Defaults to PeekLock mode.

Methods

CloseAsync(CancellationToken)

Performs the task needed to clean up resources used by the ServiceBusProcessor. Any in-flight message handlers will be awaited. Once all message handling has completed, the underlying links will be closed. After this point, the method will return. This is equivalent to calling DisposeAsync().

DisposeAsync()

Performs the task needed to clean up resources used by the ServiceBusProcessor. Any in-flight message handlers will be awaited. Once all message handling has completed, the underlying links will be closed. After this point, the method will return. This is equivalent to calling CloseAsync(CancellationToken).

OnProcessErrorAsync(ProcessErrorEventArgs)

Invokes the error event handler when an error has occurred during processing. This method can be overridden to raise an event manually for testing purposes.

OnProcessMessageAsync(ProcessMessageEventArgs)

Invokes the process message event handler after a message has been received. This method can be overridden to raise an event manually for testing purposes.

StartProcessingAsync(CancellationToken)

Signals the processor to begin processing messages. Should this method be called while the processor is already running, an InvalidOperationException is thrown.

StopProcessingAsync(CancellationToken)

Signals the processor to stop processing messaging. Should this method be called while the processor is not running, no action is taken. This method will not close the underlying receivers, but will cause the receivers to stop receiving. Any in-flight message handlers will be awaited, and this method will not return until all in-flight message handlers have returned. To close the underlying receivers, CloseAsync(CancellationToken) should be called. If CloseAsync(CancellationToken) is called, the processor cannot be restarted. If you wish to resume processing at some point after calling this method, you can call StartProcessingAsync(CancellationToken).

UpdateConcurrency(Int32)

Updates the concurrency for the processor. This method can be used to dynamically change the concurrency of a running processor.

UpdatePrefetchCount(Int32)

Updates the prefetch count for the processor. This method can be used to dynamically change the prefetch count of a running processor.

Events

ProcessErrorAsync

The handler responsible for processing unhandled exceptions thrown while this processor is running. Implementation is mandatory.

ProcessMessageAsync

The handler responsible for processing messages received from the Queue or Subscription. Implementation is mandatory.

Applies to