Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CancellationToken if it exists in the parameter. #9690

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private async Task<HttpContent> MakeRequestAsync(IAbpMethodInvocation invocation
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)
Expand Down Expand Up @@ -306,8 +306,18 @@ protected virtual StringSegment RemoveQuotes(StringSegment input)
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Volo.Abp.Http.DynamicProxying
Expand Down Expand Up @@ -36,5 +37,7 @@ public interface IRegularTestController
Task<string> PatchValueWithHeaderAndQueryStringAsync(string headerValue, string qsValue);

Task<int> DeleteByIdAsync(int id);

Task<string> AbortRequestAsync(CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -129,6 +128,14 @@ public Task<int> DeleteByIdAsync(int id)
{
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -159,5 +159,17 @@ public async Task DeleteByIdAsync()
(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.");
}
}
}