您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ManagedMqttApplicationMessageBuilder.cs 1.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. namespace MQTTnet.Extensions.ManagedClient
  3. {
  4. public class ManagedMqttApplicationMessageBuilder
  5. {
  6. private Guid _id = Guid.NewGuid();
  7. private MqttApplicationMessage _applicationMessage;
  8. public ManagedMqttApplicationMessageBuilder WithId(Guid id)
  9. {
  10. _id = id;
  11. return this;
  12. }
  13. public ManagedMqttApplicationMessageBuilder WithApplicationMessage(MqttApplicationMessage applicationMessage)
  14. {
  15. _applicationMessage = applicationMessage;
  16. return this;
  17. }
  18. public ManagedMqttApplicationMessageBuilder WithApplicationMessage(Action<MqttApplicationMessageBuilder> builder)
  19. {
  20. if (builder == null) throw new ArgumentNullException(nameof(builder));
  21. var internalBuilder = new MqttApplicationMessageBuilder();
  22. builder(internalBuilder);
  23. _applicationMessage = internalBuilder.Build();
  24. return this;
  25. }
  26. public ManagedMqttApplicationMessage Build()
  27. {
  28. if (_applicationMessage == null)
  29. {
  30. throw new InvalidOperationException("The ApplicationMessage cannot be null.");
  31. }
  32. return new ManagedMqttApplicationMessage
  33. {
  34. Id = _id,
  35. ApplicationMessage = _applicationMessage
  36. };
  37. }
  38. }
  39. }