public Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs eventArgs) =>
        Task.Run(new Func <Task>(async() =>
        {
            try
            {
                string topic = eventArgs.ApplicationMessage.Topic;
                if (string.IsNullOrWhiteSpace(topic) == false)
                {
                    var payload = Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload);
                    var qos     = eventArgs.ApplicationMessage.QualityOfServiceLevel;
                    var retain  = eventArgs.ApplicationMessage.Retain;
                    Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
                    Console.WriteLine($"+ Topic = {topic}");
                    Console.WriteLine($"+ Payload = {payload}");
                    Console.WriteLine($"+ QoS = {qos}");
                    Console.WriteLine($"+ Retain = {retain}");

                    var message = new Message()
                    {
                        ReceivedAt = DateTime.Now,
                        Topic      = topic,
                        Payload    = payload,
                        Qos        = (uint)qos,
                        Retain     = retain
                    };
                    await _httpClient.SendMessageAsync(message);
                    await _dbContext.Messages.AddAsync(message);
                    await _dbContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message, ex);
            }
        }));
示例#2
0
        public async Task Endpoint(HttpContext context,
                                   CalculationContext dataContext)
        {
            int  count = int.Parse((string)context.Request.RouteValues["count"]);
            long total = dataContext.Calculations
                         .FirstOrDefault(c => c.Count == count)?.Result ?? 0;

            if (total == 0)
            {
                for (int i = 1; i <= count; i++)
                {
                    total += i;
                }
                dataContext.Calculations !
                .Add(new Calculaton()
                {
                    Count = count, Result = total
                });
                await dataContext.SaveChangesAsync();
            }
            string totalString = $"({ DateTime.Now.ToLongTimeString() }) {total}";
            await context.Response.WriteAsync(
                $"({DateTime.Now.ToLongTimeString()}) Total for {count}"
                + $" values:\n{totalString}\n");
        }