コード例 #1
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (Module != null)
         {
             Module.Dispose();
             Module = null;
         }
     }
 }
コード例 #2
0
        // private async Task<MessageResponse> OnInputMessageReceived(Message msg, object ctx)
        // {
        //
        // }
        protected virtual async Task AzureConnectionInitAsync <M>() where M : AzureModuleBase, new()
        {
            await Task.Run(async() =>
            {
                try
                {
                    Module = new M();
                    await Module.AzureModuleInitAsync(this);
                }
                catch (Exception e)
                {
                    Log.WriteLine("AzureConnectionInitAsync ModuleInit lambda exception {0}", e.ToString());
                    Environment.Exit(1);     // failfast
                }
            });

            Log.WriteLine("Azure base generic connection Initialized");
        }
コード例 #3
0
        //NOTE: actual work for module init split in 2 parts to allow derived classes to attach additional
        // message handlers prior to the module client open
        private async Task AzureModuleInitBeginAsync()
        {
            AmqpTransportSettings[] settings = new AmqpTransportSettings[2];
            settings[0]                  = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
            settings[0].OpenTimeout      = TimeSpan.FromSeconds(AzureConnectionBase.DEFAULT_NETWORK_TIMEOUT);
            settings[0].OperationTimeout = TimeSpan.FromSeconds(AzureConnectionBase.DEFAULT_NETWORK_TIMEOUT);
            settings[1]                  = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only);
            settings[1].OpenTimeout      = TimeSpan.FromSeconds(AzureConnectionBase.DEFAULT_NETWORK_TIMEOUT);
            settings[1].OperationTimeout = TimeSpan.FromSeconds(AzureConnectionBase.DEFAULT_NETWORK_TIMEOUT);
            _reportedDeviceProperties    = new TwinCollection();
            _moduleClient                = await ModuleClient.CreateFromEnvironmentAsync(settings);

            Log.WriteLine("ModuleClient Initialized");

            // set default message handler to log unexpected requests and fail them immediately without
            // blocking queues waiting for timeouts
            await AzureConnectionBase.RetryOperationAsync("Initializing default message handler", async() =>
            {
                await _moduleClient.SetMessageHandlerAsync(async(Message msg, object ctx) =>
                {
                    try {
                        AzureModuleBase m = (AzureModuleBase)ctx;
                        Log.WriteLine("Unexpected Input Message to {0} in module {1}", msg.InputName, m.ModuleId);
                        Log.WriteLine("    {0}", Encoding.UTF8.GetString(msg.GetBytes()));
                        await Task.CompletedTask;
                    }
                    catch (Exception e)
                    {
                        Log.WriteLine("AzureModuleBase Default MessageHandler lambda exception {0}", e.ToString());
                        Environment.Exit(1); // failfast
                    }
                    return(MessageResponse.Abandoned);
                }, this);
                Log.WriteLine("AzureModuleBase default msg handler set");
            });

            // set default method handler to log unexpected requests and fail them immediately without
            // blocking queues waiting for timeouts
            await AzureConnectionBase.RetryOperationAsync("Initializing default method handler", async() =>
            {
                await _moduleClient.SetMethodDefaultHandlerAsync(async(MethodRequest req, object ctx) => {
                    try {
                        AzureModuleBase m = (AzureModuleBase)ctx;
                        Log.WriteLine("Unexpected Method Request in module {0}", m.ModuleId);
                        Log.WriteLine("    {0}", req.ToString());
                        await Task.CompletedTask;
                        string result = "{\"result\":\"Unrecognized direct method: " + req.Name + "\"}";
                        return(new MethodResponse(Encoding.UTF8.GetBytes(result), 404));
                    }
                    catch (Exception e)
                    {
                        Log.WriteLine("AzureModuleBase MethodDefaultHandler lambda exception {0}", e.ToString());
                        Environment.Exit(1); // failfast
                    }
                    return(new MethodResponse(null, 404));
                }, this);
                Log.WriteLine("AzureModuleBase default method handler set");
            });

            await AzureConnectionBase.RetryOperationAsync("Set ConnectionStatusChangesHandler", async() =>
            {
                _moduleClient.SetConnectionStatusChangesHandler(async(ConnectionStatus status, ConnectionStatusChangeReason reason) =>
                {
                    try
                    {
                        await this.OnConnectionChanged(status, reason);
                    }
                    catch (Exception e)
                    {
                        Log.WriteLine("AzureModuleBase ConnectionStatusChangesHandler lambda exception {0}", e.ToString());
                        Environment.Exit(1); // failfast
                    }
                });
                Log.WriteLine("AzureModuleBase connectionstatus handler set");

                await Task.CompletedTask;
                return;
            });

            await AzureConnectionBase.RetryOperationAsync("Initializing SetDesiredPropertyUpdateCallback", async() =>
            {
                await _moduleClient.SetDesiredPropertyUpdateCallbackAsync(async(TwinCollection newDesiredProperties, object ctx) =>
                {
                    try
                    {
                        var module = (AzureModuleBase)ctx;
                        await module.OnDesiredModulePropertyChanged(newDesiredProperties);
                    }
                    catch (Exception e)
                    {
                        Log.WriteLine("AzureModuleBase DesiredPropertyUpdateCallback lambda exception {0}", e.ToString());
                        Environment.Exit(1); // failfast
                    }
                }, this);
                Log.WriteLine("AzureModuleBase desired property update handler set");
            });
        }