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

CancellationToken not working #339

Closed
opcodewriter opened this issue Aug 8, 2017 · 11 comments
Closed

CancellationToken not working #339

opcodewriter opened this issue Aug 8, 2017 · 11 comments
Labels

Comments

@opcodewriter
Copy link

opcodewriter commented Aug 8, 2017

The CancellationToken parameter defined in the interface does not seem to be used, when I call cancellationToken.Cancel() it's not throwing TaskCanceledException. The call runs until the end like when the method didn't have the parameter, or like it's ignoring it.
To see this I am simulating low network speed.

[Headers("Accept: application/json")]
interface IMyApi
{
       [Get("/users/{userId}")]
        Task<string> GetUserAsync(int userId, [Header("U")] string username, [Header("P")] string pass, CancellationToken cancellationToken);
}

According to #88, I just need to add a parameter of CancellationToken type to my interface.

UPDATE: not sure if it matters, I have a Xamarin Android app and the code which calls Refit is in a NETLibrary 1.6. and I am using target fallback:

  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <PackageTargetFallback>portable-net45+win8+wpa81+wp8</PackageTargetFallback>
  </PropertyGroup>
@Cheesebaron
Copy link
Contributor

I can't reproduce this. I tested against http://slowwly.robertomurray.co.uk/ and set that to 10 seconds. I cancelled my token after 5 seconds. It threw the TaskCancelledException just fine.

@opcodewriter
Copy link
Author

@Cheesebaron Thanks. What kind of project is it? (Xamarin, console?)

@Cheesebaron
Copy link
Contributor

I tried in a NET Core console app. I can try in a Xamarin app too.

@michelmoorlag
Copy link

You added the token to the Interface but did you add the actual token in the actual implementation of the method? I think you should be forced by the interface but nevertheless you can check that.

@opcodewriter
Copy link
Author

@Cheesebaron See my update in the description at the bottom. Thanks

@opcodewriter
Copy link
Author

@michelmoorlag I am calling with the token, yes.

@opcodewriter
Copy link
Author

opcodewriter commented Aug 8, 2017

Meh, I was trying to compile from source code so I could try to debug, but it throws errors I can't figure out how solve


1>------ Build started: Project: InterfaceStubGenerator.Core, Configuration: Debug Any CPU ------
1>InterfaceStubGenerator.Core -> D:\Refit-Cloned\refit\InterfaceStubGenerator.Core\bin\Debug\net46\InterfaceStubGenerator.Core.dll
1>InterfaceStubGenerator.cs(50,22,50,32): error CS0012: The type 'MulticastDelegate' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1>InterfaceStubGenerator.cs(51,31,51,45): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1>InterfaceStubGenerator.cs(166,71,166,91): error CS0117: 'Assembly' does not contain a definition for 'GetExecutingAssembly'
1>InterfaceStubGenerator.cs(174,61,174,69): error CS1061: 'Type' does not contain a definition for 'Assembly' and no extension method 'Assembly' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)
1>Done building project "InterfaceStubGenerator.Core.csproj" -- FAILED.

Not the best timing for the timeline of the tasks I have...

@Cheesebaron
Copy link
Contributor

Can't reproduce in a Xamarin Android project either. Just like you I have a "Core" which is NetStandard 1.6 with target fallback so forms can be installed.

Refit is added in both core and droid project.

Then I have two buttons, one for starting a task with a request. The other calling cancel on the CancellationTokenSource. Works fine, a TaskCancelledException is thrown and request seems to be cancelled too.

@Cheesebaron
Copy link
Contributor

Cheesebaron commented Aug 8, 2017

You can see how I do it here: https://github.com/Cheesebaron/refitcancel works fine.

As for building the source. I think you need VS2017 and Net Core 2.0

@opcodewriter
Copy link
Author

@Cheesebaron thanks. No idea what the heck is going on. I debugged couple of times my code and I am definitely passing the token parameter.

@Deilan
Copy link

Deilan commented Aug 29, 2018

It's worth to mention in the docs that CancellationToken is supported. Along with an example. :)

@lock lock bot added the outdated label Jun 24, 2019
@lock lock bot locked and limited conversation to collaborators Jun 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants