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.

ManagedMqttClientOptionsBuilder.cs 2.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using MQTTnet.Client;
  3. namespace MQTTnet.Extensions.ManagedClient
  4. {
  5. public class ManagedMqttClientOptionsBuilder
  6. {
  7. private readonly ManagedMqttClientOptions _options = new ManagedMqttClientOptions();
  8. private MqttClientOptionsBuilder _clientOptionsBuilder;
  9. public ManagedMqttClientOptionsBuilder WithAutoReconnectDelay(TimeSpan value)
  10. {
  11. _options.AutoReconnectDelay = value;
  12. return this;
  13. }
  14. public ManagedMqttClientOptionsBuilder WithStorage(IManagedMqttClientStorage value)
  15. {
  16. _options.Storage = value;
  17. return this;
  18. }
  19. public ManagedMqttClientOptionsBuilder WithClientOptions(IMqttClientOptions value)
  20. {
  21. if (_clientOptionsBuilder != null)
  22. {
  23. throw new InvalidOperationException("Cannot use client options builder and client options at the same time.");
  24. }
  25. _options.ClientOptions = value ?? throw new ArgumentNullException(nameof(value));
  26. return this;
  27. }
  28. public ManagedMqttClientOptionsBuilder WithClientOptions(MqttClientOptionsBuilder builder)
  29. {
  30. if (_options.ClientOptions != null)
  31. {
  32. throw new InvalidOperationException("Cannot use client options builder and client options at the same time.");
  33. }
  34. _clientOptionsBuilder = builder;
  35. return this;
  36. }
  37. public ManagedMqttClientOptionsBuilder WithClientOptions(Action<MqttClientOptionsBuilder> options)
  38. {
  39. if (options == null) throw new ArgumentNullException(nameof(options));
  40. if (_clientOptionsBuilder == null)
  41. {
  42. _clientOptionsBuilder = new MqttClientOptionsBuilder();
  43. }
  44. options(_clientOptionsBuilder);
  45. return this;
  46. }
  47. public ManagedMqttClientOptions Build()
  48. {
  49. if (_clientOptionsBuilder != null)
  50. {
  51. _options.ClientOptions = _clientOptionsBuilder.Build();
  52. }
  53. if (_options.ClientOptions == null)
  54. {
  55. throw new InvalidOperationException("The ClientOptions cannot be null.");
  56. }
  57. return _options;
  58. }
  59. }
  60. }