问题
This is more a question regarding understanding how and when Nancy might cancel an async request handler via the provided cancellationToken instance.
Basically what I am wondering about is in what conditions is the token's .IsCancellationRequested set to true - is this documented or explained somewhere?
How does Nancy handle async handlers that 'never' return / in 'time'? And regarding 'in time': is there a timeout / limit after which handlers do get cancelled?
回答1:
I know this is an old question but I was in your same situation and I think I found an answer at least if you are using Owin to host your app (using Nancy.Owin
).
The CancellationToken
comes directly from Owin via the IOwinRequest.CallCancelled
property (Nancy source code and used here). This token can be set by Owin if the request is cancelled (for example by forcibly closing the HTTP connection).
回答2:
Every CancellationToken comes from somewhere, and that somewhere is its CancellationTokenSource.
When you call CancellationTokenSource.Cancel, every token created from it is flagged.
Fun fact: CancellationToken is a struct, which means every time you pass it to a function or assign it to a variable it makes a new copy. Since the source can't keep track of all of those copies, we can't have a CancellationToken.IWasCancelled event. Instead, when you call IsCancellationRequested, the token has to asks its source.
ref: https://msdn.microsoft.com/en-us/library/system.threading.cancellationtokensource(v=vs.110).aspx
So going back to Nancy, search their source code for CancellationTokenSource and you'll find your answer. Here's the only one I saw.
https://github.com/NancyFx/Nancy/blob/8a29b0495bfac4806536327c4d78de1ee59bd513/src/Nancy/NancyEngine.cs
回答3:
That's the beauty of cancellation tokens, you don't have to know or care how they get set. That's entirely up to the person providing you the token. You just get to look at the token and see whether or not it's been set.
If you're calling a method that accepts a CancellationToken
and you want to know how to create one that you can set whenever you want, then you should be using a CancellationTokenSource
to create a token; you can use the CTS to cancel the token that it generates, or set it to be cancelled after a set period of time.
来源:https://stackoverflow.com/questions/30675061/where-is-nancys-cancellationtoken-for-async-request-handlers-coming-from-and-wh