Example #1
0
        /// <summary>
        /// The constructor to create a new ProxyClient
        /// </summary>
        /// <param name="serviceClient">The ServiceClient used to connect to the Azure IoT Hub</param>
        /// <param name="settings">The ProxyClientSettings to configure the proxy client</param>
        /// <param name="logger">The ILogger instance to perform application logging</param>
        public ProxyClient(ServiceClient serviceClient, ProxyClientSettings settings, ILogger <ProxyClient> logger)
        {
            this.serviceClient = serviceClient ?? throw new ArgumentNullException(nameof(serviceClient));
            this.logger        = logger ?? throw new ArgumentNullException(nameof(logger));
            this.settings      = settings ?? throw new ArgumentNullException(nameof(settings));

            if (string.IsNullOrEmpty(settings.DeviceId))
            {
                throw new ArgumentException("DeviceId must not be null or empty", nameof(settings.DeviceId));
            }

            if (settings.BufferSize < 8192)
            {
                throw new ArgumentException("BufferSize must not be less than 8192 bytes", nameof(settings.BufferSize));
            }

            if (settings.BufferSize > 65536)
            {
                throw new ArgumentException("BufferSize must not be greater than 65536 bytes", nameof(settings.BufferSize));
            }
        }
Example #2
0
        /// <summary>
        /// Configures and runs the proxy
        /// </summary>
        /// <param name="options">The parsed CLI arguments</param>
        /// <param name="cancellationToken">Token used for cancelling this operation</param>
        /// <returns>An awaitable async task</returns>
        private static async Task RunAsync(CommandLineOptions options, CancellationToken cancellationToken)
        {
            // Creating proxy settings instance
            var settings = new ProxyClientSettings
            {
                DeviceId   = options.DeviceId,
                LocalPort  = options.Port,
                BufferSize = options.BufferSize
            };

            // Create typed logger instance (usually done by DI)
            var logger = CommandLineLoggerFactory.CreateLogger <ProxyClient>(options.Verbosity);

            // Initializing the ServiceClient instance used to connect to the Azure IoT Hub
            using var serviceClient = ServiceClient.CreateFromConnectionString(options.ConnectionString, TransportType.Amqp);

            // Creating ProxyClient instance
            var proxyClient = new ProxyClient(serviceClient, settings, logger);

            // Starting the proxy client
            await proxyClient.ConnectProxyAsync(cancellationToken);
        }