Browse Source

Use `CancellationToken` if it exists in the parameter.

pull/9690/head
maliming 5 years ago
parent
commit
cc3fc18adb
  1. 7
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs
  2. 3
      framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/IRegularTestController.cs
  3. 10
      framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs
  4. 13
      framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs

7
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs

@ -174,7 +174,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
var response = await client.SendAsync( var response = await client.SendAsync(
requestMessage, requestMessage,
HttpCompletionOption.ResponseHeadersRead /*this will buffer only the headers, the content will be used as a stream*/, HttpCompletionOption.ResponseHeadersRead /*this will buffer only the headers, the content will be used as a stream*/,
GetCancellationToken() GetCancellationToken(invocation)
); );
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
@ -306,9 +306,10 @@ namespace Volo.Abp.Http.Client.DynamicProxying
return input; return input;
} }
protected virtual CancellationToken GetCancellationToken() protected virtual CancellationToken GetCancellationToken(IAbpMethodInvocation invocation)
{ {
return CancellationTokenProvider.Token; var cancellationToken = invocation.Arguments.LastOrDefault(x => x is CancellationToken);
return (CancellationToken?)cancellationToken ?? CancellationTokenProvider.Token;
} }
} }
} }

3
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/IRegularTestController.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Volo.Abp.Http.DynamicProxying namespace Volo.Abp.Http.DynamicProxying
@ -36,5 +37,7 @@ namespace Volo.Abp.Http.DynamicProxying
Task<string> PatchValueWithHeaderAndQueryStringAsync(string headerValue, string qsValue); Task<string> PatchValueWithHeaderAndQueryStringAsync(string headerValue, string qsValue);
Task<int> DeleteByIdAsync(int id); Task<int> DeleteByIdAsync(int id);
Task AbortRequestAsync(CancellationToken cancellationToken);
} }
} }

10
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs

@ -1,11 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.UI;
namespace Volo.Abp.Http.DynamicProxying namespace Volo.Abp.Http.DynamicProxying
{ {
@ -129,6 +128,13 @@ namespace Volo.Abp.Http.DynamicProxying
{ {
return Task.FromResult(id + 1); return Task.FromResult(id + 1);
} }
[HttpGet]
[Route("abort-request")]
public async Task AbortRequestAsync(CancellationToken cancellationToken)
{
await Task.Delay(100, cancellationToken);
}
} }
public class Car public class Car

13
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs

@ -1,10 +1,10 @@
using System; using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Shouldly; using Shouldly;
using Volo.Abp.Http.Client; using Volo.Abp.Http.Client;
using Volo.Abp.Http.Localization;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Xunit; using Xunit;
@ -159,5 +159,14 @@ namespace Volo.Abp.Http.DynamicProxying
(await _controller.DeleteByIdAsync(42)).ShouldBe(43); (await _controller.DeleteByIdAsync(42)).ShouldBe(43);
} }
[Fact]
public async Task AbortRequestAsync()
{
var cts = new CancellationTokenSource();
cts.CancelAfter(10);
var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await _controller.AbortRequestAsync(cts.Token));
exception.InnerException.InnerException.Message.ShouldBe("The client aborted the request.");
}
} }
} }

Loading…
Cancel
Save