Use cancellation token in repository and web fie system (http call can cancelled) (#471)

Co-authored-by: cmichel <cmichel@interparking.com>
This commit is contained in:
Cédric Michel
2020-11-30 18:21:19 +01:00
committed by GitHub
parent 2502e01efd
commit 6041a1f183
19 changed files with 95 additions and 97 deletions

View File

@@ -5,6 +5,7 @@ using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Infrastructure.Data
@@ -23,58 +24,59 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
_dbContext = dbContext;
}
public virtual async Task<T> GetByIdAsync(int id)
public virtual async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().FindAsync(id);
var keyValues = new object[] { id };
return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken);
}
public async Task<IReadOnlyList<T>> ListAllAsync()
public async Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().ToListAsync();
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
}
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec)
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.ToListAsync();
return await specificationResult.ToListAsync(cancellationToken);
}
public async Task<int> CountAsync(ISpecification<T> spec)
public async Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.CountAsync();
return await specificationResult.CountAsync(cancellationToken);
}
public async Task<T> AddAsync(T entity)
public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
{
await _dbContext.Set<T>().AddAsync(entity);
await _dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync(cancellationToken);
return entity;
}
public async Task UpdateAsync(T entity)
public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync(cancellationToken);
}
public async Task DeleteAsync(T entity)
public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().Remove(entity);
await _dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync(cancellationToken);
}
public async Task<T> FirstAsync(ISpecification<T> spec)
public async Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.FirstAsync();
return await specificationResult.FirstAsync(cancellationToken);
}
public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec)
public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.FirstOrDefaultAsync();
return await specificationResult.FirstOrDefaultAsync(cancellationToken);
}
private IQueryable<T> ApplySpecification(ISpecification<T> spec)

View File

@@ -1,15 +1,16 @@
using System;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.Infrastructure.Data;
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.Infrastructure.Data;
namespace Microsoft.eShopWeb.Infrastructure.Services
{
public class WebFileSystem: IFileSystem
public class WebFileSystem : IFileSystem
{
private readonly HttpClient _httpClient;
private readonly string _url;
@@ -22,9 +23,9 @@ namespace Microsoft.eShopWeb.Infrastructure.Services
_httpClient.DefaultRequestHeaders.Add("auth-key", AUTH_KEY);
}
public async Task<bool> SavePicture(string pictureName, string pictureBase64)
public async Task<bool> SavePicture(string pictureName, string pictureBase64, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(pictureBase64) || !await UploadFile(pictureName, Convert.FromBase64String(pictureBase64)))
if (string.IsNullOrEmpty(pictureBase64) || !await UploadFile(pictureName, Convert.FromBase64String(pictureBase64), cancellationToken))
{
return false;
}
@@ -32,26 +33,26 @@ namespace Microsoft.eShopWeb.Infrastructure.Services
return true;
}
private async Task<bool> UploadFile(string fileName, byte[] fileData)
private async Task<bool> UploadFile(string fileName, byte[] fileData, CancellationToken cancellationToken)
{
if (!fileData.IsValidImage(fileName))
{
return false;
}
return await UploadToWeb(fileName, fileData);
return await UploadToWeb(fileName, fileData, cancellationToken);
}
private async Task<bool> UploadToWeb(string fileName, byte[] fileData)
private async Task<bool> UploadToWeb(string fileName, byte[] fileData, CancellationToken cancellationToken)
{
var request = new FileItem
{
DataBase64 = Convert.ToBase64String(fileData),
DataBase64 = Convert.ToBase64String(fileData),
FileName = fileName
};
var content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
using var message = await _httpClient.PostAsync(_url, content);
using var message = await _httpClient.PostAsync(_url, content, cancellationToken);
if (!message.IsSuccessStatusCode)
{
return false;