Class CommandInvoker<TReq, TResp>
- Namespace
- Azure.Iot.Operations.Protocol.RPC
- Assembly
- Azure.Iot.Operations.Protocol.dll
public abstract class CommandInvoker<TReq, TResp> : IAsyncDisposable where TReq : class where TResp : class
Type Parameters
TReqTResp
- Inheritance
-
CommandInvoker<TReq, TResp>
- Implements
- Derived
- Inherited Members
Constructors
CommandInvoker(ApplicationContext, IMqttPubSubClient, string, IPayloadSerializer)
public CommandInvoker(ApplicationContext applicationContext, IMqttPubSubClient mqttClient, string commandName, IPayloadSerializer serializer)
Parameters
applicationContextApplicationContextmqttClientIMqttPubSubClientcommandNamestringserializerIPayloadSerializer
Properties
RequestTopicPattern
public string RequestTopicPattern { get; init; }
Property Value
ResponseTopicPattern
If provided, this topic pattern will be used for command response topic.
public string? ResponseTopicPattern { get; set; }
Property Value
Remarks
If not provided, and no value is provided for ResponseTopicPrefix or ResponseTopicSuffix, the default pattern used will be clients/{mqtt client id}/{request topic pattern}.
ResponseTopicPrefix
The prefix to use in the command response topic. This value is ignored if ResponseTopicPattern is set.
public string? ResponseTopicPrefix { get; set; }
Property Value
Remarks
If no prefix or suffix is specified, and no value is provided in ResponseTopicPattern, then this value will default to "clients/{invokerClientId}" for security purposes.
If a prefix and/or suffix are provided, then the response topic will use the format: {prefix}/{command request topic}/{suffix}.
ResponseTopicSuffix
The suffix to use in the command response topic. This value is ignored if ResponseTopicPattern is set.
public string? ResponseTopicSuffix { get; set; }
Property Value
Remarks
If no suffix is specified, then the command response topic won't include a suffix.
If a prefix and/or suffix are provided, then the response topic will use the format: {prefix}/{command request topic}/{suffix}.
TopicNamespace
public string? TopicNamespace { get; set; }
Property Value
TopicTokenMap
The topic token replacement map that this command invoker will use by default. Generally, this will include the token values for topic tokens such as "modelId" which should be the same for the duration of this command invoker's lifetime.
public Dictionary<string, string> TopicTokenMap { get; protected set; }
Property Value
Remarks
Tokens replacement values can also be specified per-method invocation by specifying the additionalTopicToken map in InvokeCommandAsync(TReq, CommandRequestMetadata?, Dictionary<string, string>?, TimeSpan?, CancellationToken).
Methods
DisposeAsync()
Asynchronously dispose this object, but not the underlying mqtt client.
public ValueTask DisposeAsync()
Returns
Remarks
Users are advised to call StopAsync(CancellationToken) prior to this in order to cleanup any MQTT subscriptions that this client has.
To also dispose the underlying mqtt client, use DisposeAsync(bool).
DisposeAsync(bool, CancellationToken)
Asynchronously dispose this object and optionally dispose the underlying mqtt client as well.
public ValueTask DisposeAsync(bool disposing, CancellationToken cancellationToken = default)
Parameters
disposingboolIf true, this call will dispose the underlying mqtt client. If false, this call will not dispose the underlying mqtt client.
cancellationTokenCancellationToken
Returns
Remarks
Users are advised to call StopAsync(CancellationToken) prior to this in order to cleanup any MQTT subscriptions that this client has.
DisposeAsync(CancellationToken)
Asynchronously dispose this object, but not the underlying mqtt client.
public ValueTask DisposeAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationToken
Returns
Remarks
Users are advised to call StopAsync(CancellationToken) prior to this in order to cleanup any MQTT subscriptions that this client has.
To also dispose the underlying mqtt client, use DisposeAsync(bool, CancellationToken).
DisposeAsyncCore(bool, CancellationToken)
protected virtual ValueTask DisposeAsyncCore(bool disposing, CancellationToken cancellationToken)
Parameters
disposingboolcancellationTokenCancellationToken
Returns
InvokeCommandAsync(TReq, CommandRequestMetadata?, Dictionary<string, string>?, TimeSpan?, CancellationToken)
Invoke the specified command.
public Task<ExtendedResponse<TResp>> InvokeCommandAsync(TReq request, CommandRequestMetadata? metadata = null, Dictionary<string, string>? additionalTopicTokenMap = null, TimeSpan? commandTimeout = null, CancellationToken cancellationToken = default)
Parameters
requestTReqThe payload of command request.
metadataCommandRequestMetadataThe metadata of the command request.
additionalTopicTokenMapDictionary<string, string>The topic token replacement map to use in addition to TopicTokenMap. If this map contains any keys that TopicTokenMap also has, then values specified in this map will take precedence.
commandTimeoutTimeSpan?How long to wait for a command response. Note that each command executor also has a configurable timeout value that may be shorter than this value. ExecutionTimeout
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<ExtendedResponse<TResp>>
The command response including the command response metadata
StopAsync(CancellationToken)
Unsubscribe from any MQTT topics that this client subscribed to
public Task StopAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token
Returns
Remarks
This operation does require the underlying MQTT client to be connected to complete, so users are advised to pass in a cancellation token to protect against the case where the underlying MQTT client gets disconnected and takes an unexpectedly long time to reconnect.