public string Id { get; }
public Guid TaskId { get; }
private readonly ILogger _logger;
private readonly IOptions<AppConfig> _appConfig;
public MetacommerceService(ILogger<MetacommerceService> logger, IOptions<AppConfig> appConfig)
{
Id = "metacommerce";
TaskId = Guid.NewGuid();
_logger = logger;
_appConfig = appConfig;
}
public async Task<FetchResult> Fetch(DateTime startDate, DateTime finishDate, int dbRegionId, string marketId, string regionId)
{
FetchResult result = new FetchResult(dbRegionId);
try
{
DateTime start = DateTime.Now;
_logger.LogInformation($"{Id} ({TaskId.ToString()}) Загрузка данных с {startDate.ToString()} по {finishDate.ToString()} marketId: {marketId} regionId: {regionId}");
...
using (HttpClient client = new HttpClient())
{
var content = new StringContent(JsonSerializer.Serialize(prms), Encoding.UTF8, "application/json");
var response = await client.PostAsync($"{_appConfig.Value.MetacommerceUrl}?ApiKey={_appConfig.Value.MetacommerceApiKey}", content);
var body = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions();
options.Converters.Add(new DateTimeConverter());
ServiceResult res = JsonSerializer.Deserialize<ServiceResult>(body, options);
...
}
DateTime end = DateTime.Now;
_logger.LogInformation($"{Id} ({TaskId.ToString()}) Завершена загрузка данных, прошло {new TimeSpan(end.Subtract(start).Ticks).TotalSeconds} сек.");
}
catch (Exception ex)
{
_logger.LogError(ex, "Исключение");
}
result.ServiceId = Id;
result.TaskId = TaskId;
return result;
}
}
pastebin.com
Обсуждают сегодня