azure-webjobsazure-webjobssdk

Intercept all webjob method/function calls


Is there a way to intercept a WebJob function call? I'd like to wrap it once rather than littering my code base (a dozen projects and hundreds of triggers) with repeated code to analyze the function call.

I'm familiar with IJobActivator, and am looking for something similar with regards to intercepting a method/function call.


Solution

  • Is there a way to intercept a WebJob function call?

    You could use Function Filters which allow you to encapsulate common logic to be shared across many different functions.

    Currently it supports two filter type: Invocation Filters and Exception Filters.

    The Invocation Filters have both Executing and Executed methods that are called immediately before and immediately after the target job function is invoked.

    You could apply filters globally by adding filters to the JobHostConfiguration service collection with following code:

    var extensions = config.GetService<IExtensionRegistry>();
    extensions.RegisterExtension<IFunctionInvocationFilter>(myFilter);
    

    For more details, you could read this wiki to learn.