1

I am working on Web api 2 (.NET Core 2). I need to develop some wrapper kind of web api which calls some third party web api. So my code will need Httpclient to consume third party web api and it will serve as web api itself as well.

Some of the third party web api needs Hmac Authentication so I need to customize my Authorization header and add hash value to it.

Initially I was thinking to create middleware for this but then read about delegating handler and now I somehow confused between delegating/message handler and middleware in web api.

I also read that delegating handler are gone in .net core (Registering a new DelegatingHandler in ASP.NET Core Web API) but then HttpClientFactory is available in .net core which takes delegating handler.(https://www.stevejgordon.co.uk/httpclientfactory-aspnetcore-outgoing-request-middleware-pipeline-delegatinghandlers) Could anybody clarify?

Could anybody please suggest that for my scenario(implementing Hamc Authentication to call third party web api)
1) What should I use delegating handler or middleware?
2) If delegating handler is not gone for .net core, When delegating handler should be used and when middleware should be used?

vibs
  • 115
  • 2
  • 14

1 Answers1

1

The way I understand it is,

that you use DelegatingHandler, for your outbound http clients; Meaning, the http clients you use to send a request to an external service.

Whereas, the Middleware, is utilised for requests you receive; Meaning, someone external sends a request to your api.

But I agree, it is all very confusing. From how hard it is to find any useful info on using DelegatingHandlers as a controller middleware in dotnet core, I would say that Microsoft is very much phasing out the DelegatingHandler with the Middleware when it comes into incoming requests.

I wanted to utilise the DelgatingHandler as a way to log both the incoming messages as well as any request sent to external apis, but I ended up using a DelegatingHandler for the outbound requests, and a middleware for inbound requests.

As they described in their wiki:

"If your app is using custom HTTP modules or HTTP handlers, you'll need a plan to migrate them to ASP.NET Core. The most likely replacement in ASP.NET Core is middleware." - https://learn.microsoft.com/en-us/dotnet/architecture/porting-existing-aspnet-apps/middleware-modules-handlers#aspnet-modules-and-handlers

suuunly
  • 11
  • 2