Added missing ConfigureAwait(false) & fixed deadlock on ValidationApiException.Content

This commit is contained in:
Steve Ward 2018-11-26 22:01:42 +00:00
Родитель 7a387ed26d
Коммит 6d3a5f4742
4 изменённых файлов: 28 добавлений и 20 удалений

Просмотреть файл

@ -429,7 +429,7 @@ namespace Refit.Tests
Assert.Equal("theObject", parts[0].Headers.ContentDisposition.Name);
Assert.Null(parts[0].Headers.ContentDisposition.FileName);
Assert.Equal(mediaType, parts[0].Headers.ContentType.MediaType);
var result0 = await serializer.DeserializeAsync<ModelObject>(parts[0]);
var result0 = await serializer.DeserializeAsync<ModelObject>(parts[0]).ConfigureAwait(false);
Assert.Equal(model1.Property1, result0.Property1);
Assert.Equal(model1.Property2, result0.Property2);
}
@ -477,7 +477,7 @@ namespace Refit.Tests
Assert.Equal("theObjects", parts[0].Headers.ContentDisposition.Name);
Assert.Null(parts[0].Headers.ContentDisposition.FileName);
Assert.Equal(mediaType, parts[0].Headers.ContentType.MediaType);
var result0 = await serializer.DeserializeAsync<ModelObject>( parts[0]);
var result0 = await serializer.DeserializeAsync<ModelObject>( parts[0]).ConfigureAwait(false);
Assert.Equal(model1.Property1, result0.Property1);
Assert.Equal(model1.Property2, result0.Property2);
@ -485,7 +485,7 @@ namespace Refit.Tests
Assert.Equal("theObjects", parts[1].Headers.ContentDisposition.Name);
Assert.Null(parts[1].Headers.ContentDisposition.FileName);
Assert.Equal(mediaType, parts[1].Headers.ContentType.MediaType);
var result1 = await serializer.DeserializeAsync<ModelObject>(parts[1]);
var result1 = await serializer.DeserializeAsync<ModelObject>(parts[1]).ConfigureAwait(false);
Assert.Equal(model2.Property1, result1.Property1);
Assert.Equal(model2.Property2, result1.Property2);
}
@ -534,7 +534,7 @@ namespace Refit.Tests
Assert.Equal("theObjects", parts[0].Headers.ContentDisposition.Name);
Assert.Null(parts[0].Headers.ContentDisposition.FileName);
Assert.Equal("application/json", parts[0].Headers.ContentType.MediaType);
var result0 = JsonConvert.DeserializeObject<ModelObject>(await parts[0].ReadAsStringAsync());
var result0 = JsonConvert.DeserializeObject<ModelObject>(await parts[0].ReadAsStringAsync().ConfigureAwait(false));
Assert.Equal(model1.Property1, result0.Property1);
Assert.Equal(model1.Property2, result0.Property2);
@ -542,14 +542,14 @@ namespace Refit.Tests
Assert.Equal("theObjects", parts[1].Headers.ContentDisposition.Name);
Assert.Null(parts[1].Headers.ContentDisposition.FileName);
Assert.Equal("application/json", parts[1].Headers.ContentType.MediaType);
var result1 = JsonConvert.DeserializeObject<ModelObject>(await parts[1].ReadAsStringAsync());
var result1 = JsonConvert.DeserializeObject<ModelObject>(await parts[1].ReadAsStringAsync().ConfigureAwait(false));
Assert.Equal(model2.Property1, result1.Property1);
Assert.Equal(model2.Property2, result1.Property2);
Assert.Equal("anotherModel", parts[2].Headers.ContentDisposition.Name);
Assert.Null(parts[2].Headers.ContentDisposition.FileName);
Assert.Equal("application/json", parts[2].Headers.ContentType.MediaType);
var result2 = JsonConvert.DeserializeObject<AnotherModel>(await parts[2].ReadAsStringAsync());
var result2 = JsonConvert.DeserializeObject<AnotherModel>(await parts[2].ReadAsStringAsync().ConfigureAwait(false));
Assert.Equal(2, result2.Foos.Length);
Assert.Equal("bar1", result2.Foos[0]);
Assert.Equal("bar2", result2.Foos[1]);
@ -567,7 +567,7 @@ namespace Refit.Tests
Assert.Equal("anEnum", parts[4].Headers.ContentDisposition.Name);
Assert.Null(parts[4].Headers.ContentDisposition.FileName);
Assert.Equal("application/json", parts[4].Headers.ContentType.MediaType);
var result4 = JsonConvert.DeserializeObject<AnEnum>(await parts[4].ReadAsStringAsync());
var result4 = JsonConvert.DeserializeObject<AnEnum>(await parts[4].ReadAsStringAsync().ConfigureAwait(false));
Assert.Equal(AnEnum.Val2, result4);
Assert.Equal("aString", parts[5].Headers.ContentDisposition.Name);
@ -579,7 +579,7 @@ namespace Refit.Tests
Assert.Equal("anInt", parts[6].Headers.ContentDisposition.Name);
Assert.Null(parts[6].Headers.ContentDisposition.FileName);
Assert.Equal("application/json", parts[6].Headers.ContentType.MediaType);
var result6 = JsonConvert.DeserializeObject<int>(await parts[6].ReadAsStringAsync());
var result6 = JsonConvert.DeserializeObject<int>(await parts[6].ReadAsStringAsync().ConfigureAwait(false));
Assert.Equal(42, result6);
}
@ -630,7 +630,7 @@ namespace Refit.Tests
Assert.Equal("myName", parts[0].Headers.ContentDisposition.Name);
Assert.Equal("myFileName", parts[0].Headers.ContentDisposition.FileName);
Assert.Equal("application/custom", parts[0].Headers.ContentType.MediaType);
var result0 = await parts[0].ReadAsStringAsync();
var result0 = await parts[0].ReadAsStringAsync().ConfigureAwait(false);
Assert.Equal("some text", result0);
}
};

Просмотреть файл

@ -54,12 +54,14 @@ namespace Refit
try
{
if (response.Content.Headers.ContentType.MediaType.Equals("application/problem+json"))
{
exception = ValidationApiException.Create(exception);
}
exception.ContentHeaders = response.Content.Headers;
exception.Content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (response.Content.Headers.ContentType.MediaType.Equals("application/problem+json"))
{
exception = await ValidationApiException.Create(exception).ConfigureAwait(false);
}
response.Content.Dispose();
}
catch

Просмотреть файл

@ -222,7 +222,7 @@ namespace Refit
throw new InvalidOperationException("BaseAddress must be set on the HttpClient instance");
var factory = BuildRequestFactoryForMethod(restMethod, client.BaseAddress.AbsolutePath, restMethod.CancellationToken != null);
var rq = await factory(paramList);
var rq = await factory(paramList).ConfigureAwait(false);
HttpResponseMessage resp = null;
HttpContent content = null;
var disposeResponse = true;
@ -575,12 +575,12 @@ namespace Refit
{
foreach (var item in enumerable)
{
await AddMultipartItemAsync(multiPartContent, itemName, parameterName, item);
await AddMultipartItemAsync(multiPartContent, itemName, parameterName, item).ConfigureAwait(false);
}
}
else
{
await AddMultipartItemAsync(multiPartContent, itemName, parameterName, itemValue);
await AddMultipartItemAsync(multiPartContent, itemName, parameterName, itemValue).ConfigureAwait(false);
}
}
@ -671,7 +671,7 @@ namespace Refit
throw new InvalidOperationException("BaseAddress must be set on the HttpClient instance");
var factory = BuildRequestFactoryForMethod(restMethod, client.BaseAddress.AbsolutePath, restMethod.CancellationToken != null);
var rq = await factory(paramList);
var rq = await factory(paramList).ConfigureAwait(false);
var ct = CancellationToken.None;

Просмотреть файл

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
namespace Refit
{
@ -14,19 +15,24 @@ namespace Refit
{
}
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
/// <summary>
/// Creates a new instance of a ValidationException from an existing ApiException.
/// </summary>
/// <param name="exception">An instance of an ApiException to use to build a ValidationException.</param>
/// <returns>ValidationApiException</returns>
public static ValidationApiException Create(ApiException exception)
public static async Task<ValidationApiException> Create(ApiException exception)
#pragma warning restore VSTHRD200
{
return new ValidationApiException(exception);
return new ValidationApiException(exception)
{
Content = await exception.GetContentAsAsync<ProblemDetails>()
};
}
/// <summary>
/// The problem details of the RFC 7807 validation exception.
/// </summary>
public new ProblemDetails Content => GetContentAsAsync<ProblemDetails>().ConfigureAwait(false).GetAwaiter().GetResult();
public new ProblemDetails Content { get; private set; }
}
}