Questions tagged [delegatinghandler]
36 questions
28
votes
4 answers
Unit testing DelegatingHandler
How do I unit test a custom DelegatingHandler? I have the following but its complaining the innerHandler not set.
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://foo.com");
var handler = new FooHandler()
{
InnerHandler =…

Dr Schizo
- 4,045
- 7
- 38
- 77
13
votes
1 answer
C# HttpClient refresh token strategy
Since Microsoft recommends that the HttpClient be created once and reused throughout the life of a program, I wondering how to update DefaultRequestHeaders when for example, the token has expired and need to be refresh.
DefaultRequestHeaders is more…

Philippe Lécaillon
- 381
- 5
- 12
10
votes
1 answer
How do we log authentication token requests in ASP.NET Web API
We have an ASP.NET Web Api application which uses OAuth Bearer Tokens for authentication, for which we are trying to implement Request/Response logging.
Basically it works like this:
1. User sends request to "/authenticate" and receives an…

thiag0
- 2,199
- 5
- 31
- 51
5
votes
0 answers
Why is IHttpContextAccessor.HttpContext sometimes null?
Using .Net Core 2.2, I'm using HttpClient to call an API, along with an Http Message Handler. The message handler injects IHttpContextAccessor, but that object sometimes contains an HttpContext property that is null - I can't understand why that…

Jeremy Maddrey
- 59
- 3
5
votes
2 answers
Capture all requests to Web Api 2.0, regardless that are mapped or not
I have a Web API 2.0 running at localhost:4512 and I want to intercept all requests made to domain localhost:4512 regardless if they are handled by a specific route or not.
For instance I want to capture requests made to localhost:4512/abc.dfsada…

Tamas Ionut
- 4,240
- 5
- 36
- 59
4
votes
0 answers
Using HttpClient and Custom DelegatingHandler to check for "Unauthorized" requests
I'm currently using a DelegatingHandler to check requests if they become Unauthorized when sending to our Web API. If the response does become unauthorized, I'm currently sending a refresh token to log the user back in and then updating the…

Humpy
- 2,004
- 2
- 22
- 45
3
votes
1 answer
Verify that my DelegatingHandler was added to IHttpClientBuilder
I have code which adds a named HttpClient to the ServiceCollection and adds a DelegatingHandler to the client. Example code:
public class MyDelegatingHandler : DelegatingHandler
{
// ...
}
public static IHostBuilder CreateHostBuilder(string[]…

devklick
- 2,000
- 3
- 30
- 47
3
votes
1 answer
HttpModule vs DelegatingHandler - advantages/disadvantages?
I'm trying to log entire incoming requests and outgoing responses in an ASP.NET WebAPI project. While I'm agreed with DelegatingHandler, my employer insists on using HttpModule. How would you explain to her, why we should use DelegatingHandler and…

amiry jd
- 27,021
- 30
- 116
- 215
2
votes
1 answer
Polly "retry" throws TaskCanceledException on first failure retry attempt
I have implemented Polly in it's own "retry" HttpClient DelegateHandler in a dll written to .NET Standard 2.0. I have the Polly v7.2.3 package. My HttpClient is running separate from an HttpClientFactory since only one instance will ever exist…

Jeff Lehmer
- 241
- 1
- 5
- 17
2
votes
1 answer
Content is missing for some 404 responses
I'm trying to return custom error responses from web.api. Let it be simple string "Oops!" formatted as json. So I created simple delegating handler which replaces error responses like this:
public class ErrorMessageHandler : DelegatingHandler
{
…

Sergey Berezovskiy
- 232,247
- 41
- 429
- 459
2
votes
0 answers
Why DelegatingHandler not firing per route?
I cannot get the WebAPI DelegatingHandler to execute based on the route
public class JwtCookieHandler : DelegatingHandler
{
public static string SessionIdToken = "session-id";
async protected override…

nam vo
- 3,271
- 12
- 49
- 76
2
votes
2 answers
DelegatingHandler to cache an HttpResponseMessage
I'm trying to cache a response to a webapi endpoint requests.
I've created a DelegatingHadler that short circuits the pipeline reusing a previously generated response, and it does not work.
What am I doing wrong? or how can I do it correctly?
This…

Flavio CF Oliveira
- 5,235
- 13
- 40
- 63
2
votes
1 answer
Using multiple delegating handlers in a web API call
I have a web API call that needs to make use of 2 delegating handlers, but I never seem to get in to the second handler. The route is registered with the handlers. I am not allowed to set the handlers in the global config.
DelegatingHandler[]…

Jorn Theunissen
- 191
- 2
- 14
2
votes
1 answer
Addition of BearerToken Authorization Header in DelegatingHandler ASP.NET Web API
Why if I add authorization header with right token in my custom DelegatingHandler to the request without this header, I get the response: 401 authorization required?
public class ProxyHandler : DelegatingHandler
{
protected async override…

paulka
- 133
- 1
- 2
- 7
1
vote
2 answers
Add dynamic parameter to DelegatingHandler used within the HttpClientFactory Service
I'm trying to use a IHttpClientFactory in my application in order to efficiently manage the HTTP Clients used throughout its lifecycle.
Each of these clients need to have their bearer token set up depending on a specific Client ID which is only…

user14101378
- 13
- 2