My Quartz.net job running as a Windows service is sending only the first object instead of the entire array. I am not sure what is causing the issue - if the issue is at the Quartz job level or the Async post logic.
This is the code that sends the payload (Helper.cs):
public static async Task<List<Root2>> PostMGData()
{
var dash = "-";
string mycontent = "";
string responsePath = "";
string responseLog = "";
List<Root2> details = GetRemmitances();
List<Root2> malformedIdentities = details.Where(x => !x.clientNationalId.Contains(dash)).ToList();
List<Root2> formedIdentities = details.Where(x => x.clientNationalId.Contains(dash)).ToList();
List<Root2> fixedIdentities = new List<Root2>();
List<Root2> payload = new List<Root2>();
foreach (var identity in malformedIdentities)
{
identity.clientNationalId = identity.clientNationalId.Insert(2, "-");
fixedIdentities.Add(identity);
}
payload = formedIdentities.Concat(fixedIdentities).ToList();
JArray array = new JArray();
foreach (var tran in payload)
{
var lis = JsonConvert.SerializeObject(tran);
array.Add(lis);
}
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Add("x-api-key", apikey.ToString());
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Uri.EscapeUriString(client.BaseAddress.ToString()));
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
foreach (var tran in array)
{
request.Content = new StringContent(tran.ToString(), Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(client.BaseAddress.ToString(), request.Content);
if (response.IsSuccessStatusCode)
{
mycontent = response.Content.ReadAsStringAsync().Result;
}
else
{
// Console.WriteLine("Internal server Error");
}
}
catch (Exception ex)
{
ExceptionLogging.SendErrorToText(ex);
}
}
}
responsePath = @"C:\RBZ\";
responseLog = "RBZ_Response";
var rsesponsePath = responsePath Convert.ToString(responseLog) DateTime.Today.ToString("dd -MM-yy") ".txt";
if (!File.Exists(rsesponsePath))
{
File.Create(rsesponsePath).Dispose();
}
using (StreamWriter sw = File.AppendText(rsesponsePath))
{
sw.WriteLine(mycontent);
sw.Flush();
sw.Close();
}
return details;
}
This is how the function is invoked by Quartz.net ( RemmitanceHelperService.cs):
public class RemmitanceHelperService : IHelperService
{
private static ILogger _logger;
public RemmitanceHelperService()
{
SetUpNLog();
}
public async Task PerformService(string schedule)
{
try
{
_logger.Info($"{DateTime.Now}: The RemmitanceService() is called with {schedule} schedule");
if (!string.IsNullOrWhiteSpace(schedule))
{
PostRemmitanceData();
_logger.Info($"{DateTime.Now}: The RemmitanceService() is finished with {schedule} schedule");
}
}
catch (Exception ex)
{
ExceptionLogging.SendErrorToText(ex);
_logger.Error($"{DateTime.Now}: Exception occurred at RemmitanceService(): {ex.Message}");
throw new CustomConfigurationException(ex.Message);
}
}
private async Task PostRemmitanceData()
{
try
{
await Helper.PostMGData();
}
catch (Exception ex)
{
ExceptionLogging.SendErrorToText(ex);
}
}
}
I am not sure what's causing the job to skip looping the entire JSON array. The implementation is based on an example given here.
CodePudding user response: