I am using a Generic repository and UnitOfWork pattern in my application. I have a method called UpdateFixtureAsync where I encounter an error. Here's the code for the method:
public async Task<bool> UpdateFixtureAsync(Fixture fixtureItem)
{
var fixtureRepository = _unitOfWork.GetRepository<Fixture>();
await fixtureRepository.UpdateAsync(fixtureItem);
await _unitOfWork.CommitAsync();
return true;
}
Method is called with the following block:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
for (int i = 0; i < 7; i++)
{
await AddFixturesAsync();
await Task.Delay(1000, stoppingToken);
}
}
}
private async Task AddFixturesAsync()
{
try
{
.....
await ProcessFixturesAsync(x,y);
}
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
}
}
private async Task ProcessFixturesAsync(string x, string y)
{
using (HttpClient client = new HttpClient())
{
var rootResponse = await GetRootResponseAsync(x,y);
await ProcessRootResponseAsync(rootResponse);
}
}
private async Task<Root> GetRootResponseAsync()
{
.... not important...
}
private async Task ProcessRootResponseAsync(Root rootResponse, Sport sport)
{
foreach (var location in rootResponse.Locations)
{
await ProcessEventAsync(location);
}
}
private async Task ProcessEventAsync(Location location)
{
foreach (var ev in location.Events)
{
var fixture = await _fixtureServices.GetFixtureAsync(ev);
if (fixture == null)
{
fixture = new Fixture
{
name=ev.Name
};
await _fixtureServices.AddFixtureAsync(fixture);
}
await ProcessFixtureAsync(fixture);
}
}
private async Task ProcessFixtureAsync(Fixture fixture)
{
using (var client = new HttpClient())
{
string url="";
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
var root = JsonConvert.DeserializeObject<Root>(responseBody);
foreach (var location in root.Locations)
{
fixture.Position= location.Position;
await _fixtureServices.UpdateFixtureAsync(fixture);
}
}
}
I suspect the problem might be related to asynchronous operations, but I'm not sure where to look. I've encountered similar issues, but none seem to match my problem exactly. Any insights would be appreciated!