Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ManagedMqttApplicationMessageBuilder.cs 1.6 KiB

2 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. namespace MQTTnet.Extensions.ManagedClient
  6. {
  7. public class ManagedMqttApplicationMessageBuilder
  8. {
  9. private Guid _id = Guid.NewGuid();
  10. private MqttApplicationMessage _applicationMessage;
  11. public ManagedMqttApplicationMessageBuilder WithId(Guid id)
  12. {
  13. _id = id;
  14. return this;
  15. }
  16. public ManagedMqttApplicationMessageBuilder WithApplicationMessage(MqttApplicationMessage applicationMessage)
  17. {
  18. _applicationMessage = applicationMessage;
  19. return this;
  20. }
  21. public ManagedMqttApplicationMessageBuilder WithApplicationMessage(Action<MqttApplicationMessageBuilder> builder)
  22. {
  23. if (builder == null) throw new ArgumentNullException(nameof(builder));
  24. var internalBuilder = new MqttApplicationMessageBuilder();
  25. builder(internalBuilder);
  26. _applicationMessage = internalBuilder.Build();
  27. return this;
  28. }
  29. public ManagedMqttApplicationMessage Build()
  30. {
  31. if (_applicationMessage == null)
  32. {
  33. throw new InvalidOperationException("The ApplicationMessage cannot be null.");
  34. }
  35. return new ManagedMqttApplicationMessage
  36. {
  37. Id = _id,
  38. ApplicationMessage = _applicationMessage
  39. };
  40. }
  41. }
  42. }