Browse Source
Merge pull request #9690 from abpframework/maliming/CancellationToken
Use `CancellationToken` if it exists in the parameter.
pull/9703/head
Halil İbrahim Kalkan
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
38 additions and
6 deletions
-
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs
-
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/IRegularTestController.cs
-
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs
-
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs
|
|
|
@ -174,7 +174,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying |
|
|
|
var response = await client.SendAsync( |
|
|
|
requestMessage, |
|
|
|
HttpCompletionOption.ResponseHeadersRead /*this will buffer only the headers, the content will be used as a stream*/, |
|
|
|
GetCancellationToken() |
|
|
|
GetCancellationToken(invocation) |
|
|
|
); |
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
@ -306,8 +306,18 @@ namespace Volo.Abp.Http.Client.DynamicProxying |
|
|
|
return input; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual CancellationToken GetCancellationToken() |
|
|
|
protected virtual CancellationToken GetCancellationToken(IAbpMethodInvocation invocation) |
|
|
|
{ |
|
|
|
var cancellationTokenArg = invocation.Arguments.LastOrDefault(x => x is CancellationToken); |
|
|
|
if (cancellationTokenArg != null) |
|
|
|
{ |
|
|
|
var cancellationToken = (CancellationToken) cancellationTokenArg; |
|
|
|
if (cancellationToken != default) |
|
|
|
{ |
|
|
|
return cancellationToken; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return CancellationTokenProvider.Token; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
namespace Volo.Abp.Http.DynamicProxying |
|
|
|
@ -36,5 +37,7 @@ namespace Volo.Abp.Http.DynamicProxying |
|
|
|
Task<string> PatchValueWithHeaderAndQueryStringAsync(string headerValue, string qsValue); |
|
|
|
|
|
|
|
Task<int> DeleteByIdAsync(int id); |
|
|
|
|
|
|
|
Task<string> AbortRequestAsync(CancellationToken cancellationToken = default); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -1,11 +1,10 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Globalization; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Volo.Abp.Application.Services; |
|
|
|
using Volo.Abp.AspNetCore.Mvc; |
|
|
|
using Volo.Abp.UI; |
|
|
|
|
|
|
|
namespace Volo.Abp.Http.DynamicProxying |
|
|
|
{ |
|
|
|
@ -129,6 +128,14 @@ namespace Volo.Abp.Http.DynamicProxying |
|
|
|
{ |
|
|
|
return Task.FromResult(id + 1); |
|
|
|
} |
|
|
|
|
|
|
|
[HttpGet] |
|
|
|
[Route("abort-request")] |
|
|
|
public async Task<string> AbortRequestAsync(CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
await Task.Delay(100, cancellationToken); |
|
|
|
return "AbortRequestAsync"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public class Car |
|
|
|
|
|
|
|
@ -1,10 +1,10 @@ |
|
|
|
using System; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Microsoft.Extensions.Localization; |
|
|
|
using Shouldly; |
|
|
|
using Volo.Abp.Http.Client; |
|
|
|
using Volo.Abp.Http.Localization; |
|
|
|
using Volo.Abp.Localization; |
|
|
|
using Xunit; |
|
|
|
|
|
|
|
@ -159,5 +159,17 @@ namespace Volo.Abp.Http.DynamicProxying |
|
|
|
(await _controller.DeleteByIdAsync(42)).ShouldBe(43); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task AbortRequestAsync() |
|
|
|
{ |
|
|
|
var cts = new CancellationTokenSource(); |
|
|
|
cts.CancelAfter(10); |
|
|
|
|
|
|
|
var result = await _controller.AbortRequestAsync(default); |
|
|
|
result.ShouldBe("AbortRequestAsync"); |
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await _controller.AbortRequestAsync(cts.Token)); |
|
|
|
exception.InnerException.InnerException.Message.ShouldBe("The client aborted the request."); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|