async override protected Task <SendEventResults> SendEventsAsync(object myClient, TheSenderThing azureThing, CancellationToken cancelToken, IEnumerable <nsCDEngine.ViewModels.TheThingStore> thingUpdatesToSend, IEventConverter eventConverter) { var client = myClient as TheREST; if (client == null) { throw new Exception("Internal error: Invalid or null client"); } var results = new SendEventResults(); results.SendTasks = new List <Task>(); var messagePayloads = eventConverter.GetEventData(thingUpdatesToSend, azureThing, maxEventDataSize, false); long batchLength = 0; foreach (var msgObj in messagePayloads) { var msgString = msgObj as string; var correlationId = Guid.NewGuid(); try { var postCS = new TaskCompletionSource <bool>(); Task sendTask = postCS.Task; if (msgString != null) { var payload = Encoding.UTF8.GetBytes(msgString); client.PostRESTAsync(new Uri($"{HttpTargetUrl}"), rd => { postCS.TrySetResult(true); }, payload, "application/json", Guid.Empty, null, rd => { if (EnableLogSentPayloads) { try { string strFilePath = TheCommonUtils.cdeFixupFileName("httpsenderdata.log"); System.IO.File.AppendAllText(strFilePath, $"{{\"TimePublished\":\"{DateTimeOffset.Now:O}\", \"PLS\": {rd?.ErrorDescription}}},\r\n"); } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(95307, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(strPrometheusExporter, $"Unable to log data to file: {this.MyBaseThing?.Address} - {thingUpdatesToSend.Count()}", eMsgLevel.l3_ImportantMessage, e.ToString())); } } postCS.TrySetException(new Exception($"PostRESTAsync Failed: {rd?.ErrorDescription}")); }, null); batchLength += msgString.Length; if (EnableLogSentPayloads) { try { string strFilePath = TheCommonUtils.cdeFixupFileName("httpsenderdata.log"); System.IO.File.AppendAllText(strFilePath, $"{{\"TimePublished\":\"{DateTimeOffset.Now:O}\", \"Body\": {msgString}}},\r\n"); } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(95307, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(strPrometheusExporter, $"Unable to log data to file: {this.MyBaseThing?.Address} - {thingUpdatesToSend.Count()}", eMsgLevel.l3_ImportantMessage, e.ToString())); } } Interlocked.Increment(ref _pendingKPIs.EventScheduledCount); } else { postCS.TrySetException(new InvalidTransportEncodingException()); } results.SendTasks.Add(sendTask); if (GetPreserveOrderForSenderThing(azureThing)) { await sendTask; } } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(95307, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(strPrometheusExporter, $"Internal error: {this.MyBaseThing?.Address} - {thingUpdatesToSend.Count()}", eMsgLevel.l3_ImportantMessage, e.ToString())); } cancelToken.ThrowIfCancellationRequested(); } results.SizeSent = batchLength; return(results); }