Using Mediatr with Azure Functions

I’ve used Mediatr for some time, to reduce controller action bloat, and have spoken about it a few times, on how I use it to avoid the classic ‘xService’ pattern:

Controller Action -> ‘Service’ -> Repo

Lately, I’ve been doing a lot of work with Azure Functions, and would like to be able to use Mediatr, as I can in Core.

Up until recently, this has been hard (very hard) – due to lack of dependency injection support.

At Build 2019, Jeff Hollan announced dependency injection for Azure Functions

This new functionality makes using Mediatr with Azure Functions relatively trivial.
Simply register the dependencies in a `Startup` class which inherits `FunctionsStartup`

We can even use FluentValidator and Mediatr Pipelines:

My function can then be as simple as:

Full code / demo available on GitHub at

Try / Catch 🙁

Notice the nasty `try catch` to handle a `ValidationException`

Unfortunately, until Function Filters supports something like this – we’re somewhat stuck with it.

We could handle this in a base class. See my first draft idea on this branch:

I’m watching #1314 and hoping to be able to tidy this pattern up soon!

Leave a Reply

Your email address will not be published. Required fields are marked *