Skip to main content

LastWillAndTestamentBuilder

The LastWillAndTestamentBuilder class provides a fluent API for constructing instances of LastWillAndTestament. It allows you to set various properties for creating a Last Will and Testament message, such as the topic, payload, Quality of Service (QoS), and additional metadata.

Constructor

LastWillAndTestamentBuilder()

Initializes a new instance of the LastWillAndTestamentBuilder class.

LastWillAndTestamentBuilder();

Example:

var builder = new LastWillAndTestamentBuilder();

Methods

WithTopic(string topic)

Sets the topic for the Last Will and Testament message.

LastWillAndTestamentBuilder WithTopic(string topic);

Parameters:

  • topic (string): The topic name.

Example:

builder.WithTopic("client/disconnect");

WithPayload(byte[] payload)

Sets the payload for the Last Will and Testament message as a byte array.

LastWillAndTestamentBuilder WithPayload(byte[] payload);

Parameters:

  • payload (byte[]): The payload in bytes.

Example:

builder.WithPayload(Encoding.UTF8.GetBytes("Disconnected unexpectedly"));

WithPayload(string payload)

Sets the payload for the Last Will and Testament message as a string.

LastWillAndTestamentBuilder WithPayload(string payload);

Parameters:

  • payload (string): The payload as a string.

Example:

builder.WithPayload("Disconnected unexpectedly");

WithQualityOfServiceLevel(QualityOfService qos)

Sets the Quality of Service (QoS) level for the Last Will and Testament message.

LastWillAndTestamentBuilder WithQualityOfServiceLevel(QualityOfService qos);

Parameters:

  • qos (QualityOfService): The QoS level.

Example:

builder.WithQualityOfServiceLevel(QualityOfService.AtLeastOnce);

WithRetain(bool retain)

Sets the retain flag for the Last Will and Testament message.

LastWillAndTestamentBuilder WithRetain(bool retain);

Parameters:

  • retain (bool): Whether the message should be retained by the broker.

Example:

builder.WithRetain(true);

WithWillDelayInterval(long willDelayInterval)

Sets the delay before the Last Will and Testament message is sent.

LastWillAndTestamentBuilder WithWillDelayInterval(long willDelayInterval);

Parameters:

  • willDelayInterval (long): The delay in seconds.

Example:

builder.WithWillDelayInterval(60); // Delay of 60 seconds

WithPayloadFormatIndicator(MQTT5PayloadFormatIndicator payloadFormatIndicator)

Sets the payload format indicator.

LastWillAndTestamentBuilder WithPayloadFormatIndicator(MQTT5PayloadFormatIndicator payloadFormatIndicator);

Parameters:

  • payloadFormatIndicator (MQTT5PayloadFormatIndicator): The payload format indicator.

Example:

builder.WithPayloadFormatIndicator(MQTT5PayloadFormatIndicator.Text);

WithMessageExpiryInterval(long messageExpiryInterval)

Sets the expiry interval for the Last Will and Testament message.

LastWillAndTestamentBuilder WithMessageExpiryInterval(long messageExpiryInterval);

Parameters:

  • messageExpiryInterval (long): The expiry interval in seconds.

Example:

builder.WithMessageExpiryInterval(3600); // 1 hour expiry

WithContentType(string contentType)

Sets the content type for the Last Will and Testament message.

LastWillAndTestamentBuilder WithContentType(string contentType);

Parameters:

  • contentType (string): The content type.

Example:

builder.WithContentType("text/plain");

WithResponseTopic(string responseTopic)

Sets the response topic for the Last Will and Testament message.

LastWillAndTestamentBuilder WithResponseTopic(string responseTopic);

Parameters:

  • responseTopic (string): The response topic.

Example:

builder.WithResponseTopic("response/topic");

WithCorrelationData(byte[] correlationData)

Sets the correlation data for the Last Will and Testament message.

LastWillAndTestamentBuilder WithCorrelationData(byte[] correlationData);

Parameters:

  • correlationData (byte[]): The correlation data.

Example:

builder.WithCorrelationData(Encoding.UTF8.GetBytes("12345"));

WithUserProperty(string key, string value)

Adds a single user property to the Last Will and Testament message.

LastWillAndTestamentBuilder WithUserProperty(string key, string value);

Parameters:

  • key (string): The key of the user property.
  • value (string): The value of the user property.

Example:

builder.WithUserProperty("reason", "client disconnect");

WithUserProperties(Dictionary<string, string> properties)

Adds multiple user properties to the Last Will and Testament message.

LastWillAndTestamentBuilder WithUserProperties(Dictionary<string, string> properties);

Parameters:

  • properties (Dictionary<string, string>): A dictionary of user properties.

Example:

builder.WithUserProperties(new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
});

Build()

Builds the LastWillAndTestament instance.

LastWillAndTestament Build();

Description:
Constructs the LastWillAndTestament instance and validates the configuration.

Example:

var lwt = builder
.WithTopic("client/disconnect")
.WithPayload("Disconnected unexpectedly")
.WithQualityOfServiceLevel(QualityOfService.ExactlyOnce)
.WithRetain(true)
.Build();