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);
                }
            });
        }
Beispiel #2
0
        public void ProcessMessage(MqttMsgEventArgs payload, ConfigData config)
        {
            if (payload.Topic.StartsWith("$iothub/methods/POST/"))
            {
                var methodName = payload.Topic.Replace("$iothub/methods/POST/", "").Split('?').FirstOrDefault()?.Trim('/');
                var topicparts = payload.Topic.Split(new[] { '?' }, 2);
                var dic        = topicparts.Last().Split('&').ToDictionary(x => x.Split('=')[0], x => x.Split('=')[1]);

                string topicResponse = $"$iothub/methods/res/200/?$rid={dic["$rid"]}";
                RespondToDirectMethod(config, topicResponse);
            }
        }
 private void Client_ConnectionClosed(object sender, EventArgs e)
 {
     LogMessage($"[{this.Name}] Connection closed", "Warning");
     if (_configData.AutoReconnect == false)
     {
         var payload = new MqttMsgEventArgs("$iothub/clientproxy/", Encoding.UTF8.GetBytes("Disconnected"), false, 0, false);
         this.ForwardMessageAsync(payload).Wait();
     }
     else
     {
         Thread.Sleep(500);
         this.Connect(null, true);
     }
 }
        public void LogMessage(string message, string severity = "Info")
        {
            var payload = new MqttMsgEventArgs($"$iothub/logmessage/{severity}", Encoding.UTF8.GetBytes($"{DateTime.Now.ToLocalTime().ToString("yyyy-MM-ddTHH:MM:ss.fff")}: {message}"), false, 0, false);

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