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

Using cancellationToken returns UnsupportedMediaType #534

Closed
topagarwal opened this issue Aug 14, 2019 · 4 comments
Closed

Using cancellationToken returns UnsupportedMediaType #534

topagarwal opened this issue Aug 14, 2019 · 4 comments

Comments

@topagarwal
Copy link

topagarwal commented Aug 14, 2019

HI,

I have exiting ASP .Net core project targeting netcoreapp 2.1.
I have taken dependency on latest Aspnetcore versioning package - "Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.5" />.
Added to the startup

services.AddApiVersioning(options =>
            {
                options.ReportApiVersions = true;
                options.AssumeDefaultVersionWhenUnspecified = true;
            });

I added a new controller

 [Route("v{version:apiVersion}/api/[controller]")]
    [ApiController]
    [ApiVersion("2.0")]
    public class TestController : ControllerBase
    {
        // GET v2.0/api/test
        [HttpGet]
        public IActionResult GetTest(CancellationToken cancellationToken)
        {
            return Ok(200);
        }
    }

I get "UnsupportedMediaType" error when I try to reach this controller with a valid URL. When I remove "CancellationToken" from the controller it works fine.

Any ideas on what might be going wrong here? I also tried by upgrading my project to netCoreapp 2.2 but still the same result.

@topagarwal topagarwal changed the title CancellationToken is not handled by Apiversioning Using cancellationToken returns UnsupportedMediaType Aug 14, 2019
@commonsensesoftware commonsensesoftware self-assigned this Aug 14, 2019
@commonsensesoftware
Copy link
Collaborator

Hmm... I've never seen this before. I personally use CancellationToken all the time. There is a special model binder provided by ASP.NET Core out of the box that wires this up. API Versioning does not muck with that. I wasn't able to repro this. Do you happen to have the simplest repro you can share? That will make investigating the issue much faster. Thanks.

@kdcllc
Copy link

kdcllc commented Aug 15, 2019

It can be related to dotnet/aspnetcore#4396

@topagarwal
Copy link
Author

Thanks for the responses.
It appears that I needed to add SetCompatibilityVersion(AspNetCore.Mvc.CompatibilityVersion.Version_2_2) when adding services.AddMvc() in the startup. Before using Aspnet-api-versioning library there was no need for me to decorate my controllers with [ApiController] attribute. They were just inherited from ControllerBase class and everything was working as expected. But for versioning library to work correctly I had to add [ApiController] attribute and it triggered something that was not able to resolve CancellationToken. Setting the compatibility version fixed my issue.

@commonsensesoftware
Copy link
Collaborator

Ah ... yes. I'm glad you got that ironed out.

There was a behavioral change back in the initial 3.0 timeframe I believe it was. There is a convention mechanism to disambiguate API controllers from UI controllers. Previously this was no so easy way to do that. There were a number of people hosting both together and were frustrated by the versioning policies applied onto UI controllers.

Unfortunately, the side effect is that API-only controller projects who were not using the new API Conventions (e.g. [ApiController]) feature occasionally found things stopped working. There was no perfect answer to this issue beyond calling it out in the release notes.

The policy is defined by the IApiControllerFilter service. You can extend or replace the default policy to suite your needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants