Default CancellationTokenModelBinder behavior should provide HttpResponse.ClientDisconnectedToken

Topics: ASP.NET MVC
Jan 30, 2015 at 10:10 PM
Hi,

In looking at http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/CancellationTokenModelBinder.cs , I see that, effectively, CancellationToken.None is passed in to any CancellationToken parameter of an action. I think it would be a nicer idiom to be able to write
public async Task<ActionResult> DoViaProvidedToken(CancellationToken ct)
{
    ViewBag.Stuff = await PerformLongTask(ct);
    return View();
}
instead of
public async Task<ActionResult> DoViaToken()
{
    ViewBag.Stuff = await PerformLongTask(Response.ClientDisconnectedToken);
    return View();
}
There are two issues I have with the latter. First, it requires me to type Response.ClientDisconnectedToken all over the place. Second, it goes against the implicit guidelines for making testable Controllers by coupling the controller with HttpResponse.

I know I can override the default model binding stuff. In fact, I’ve just stuffed my own simple model binder that looks like
public class CancellationTokenModelBinder
: IModelBinder
{
    public object BindModel(
        ControllerContext controllerContext,
        ModelBindingContext modelBindingContext)
    {
        return controllerContext.HttpContext.Response.ClientDisconnectedToken;
    }
}
into ModelBinders.Binders here to get this effect.

But, why isn’t this the default? ;-)