Esempio n. 1
0
        private void Client_ConnectionClosed(object sender, EventArgs e)
        {
            LogMessage($"[{this.Name}] Connection closed", "Warning");
            var payload = new MqttMsgEventArgs("$iothub/clientproxy/", Encoding.UTF8.GetBytes("Disconnected"), false, 0, false);

            this.ForwardMessageAsync(payload).Wait();
        }
Esempio n. 2
0
        private async Task ForwardMessageAsync(MqttMsgEventArgs e)
        {
            await Task.Run(() =>
            {
                ChannelFactory <IGenericOneWayContract> factory = null;
                try
                {
                    var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
                    var se      = new ServiceEndpoint(ContractDescription.GetContract(typeof(IGenericOneWayContract)), binding, new EndpointAddress(this._configData.TesterAddress));
                    factory     = new ChannelFactory <IGenericOneWayContract>(se);
                    var channel = factory.CreateChannel();

                    using (var scope = new OperationContextScope((IContextChannel)channel))
                    {
                        var message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "*", JsonConvert.SerializeObject(e));
                        message.Headers.Add(MessageHeader.CreateHeader(ConfigData.XName.LocalName, ConfigData.XName.NamespaceName, this._configData));
                        channel.ProcessMessage(message);

                        Trace.WriteLine("VirtualService: --- Message has been sent to tester ---");
                    }
                    factory.Close();
                }
                catch (CommunicationException ex)
                {
                    if (factory != null)
                    {
                        if (factory.State == CommunicationState.Faulted)
                        {
                            factory.Abort();
                        }
                        else if (factory.State != CommunicationState.Closed)
                        {
                            factory.Close();
                        }
                        factory = null;
                    }
                    Trace.WriteLine(ex.InnerException == null ? ex.Message : ex.InnerException.Message);
                }
                catch (Exception ex)
                {
                    if (factory != null)
                    {
                        if (factory.State == CommunicationState.Faulted)
                        {
                            factory.Abort();
                        }
                        else if (factory.State != CommunicationState.Closed)
                        {
                            factory.Close();
                        }
                        factory = null;
                    }
                    Trace.WriteLine(ex.InnerException == null ? ex.Message : ex.InnerException.Message);
                }
            });
        }
Esempio n. 3
0
        public void LogMessage(string message, string severity = "Info")
        {
            var payload = new MqttMsgEventArgs($"$iothub/logmessage/{severity}", Encoding.UTF8.GetBytes($"{DateTime.Now.ToLocalTime().ToString()}: {message}"), false, 0, false);

            this.ForwardMessageAsync(payload).Wait();
        }