4

I'm looking for a way to add a standard azure function to every azure function project. This is a requirement of our environment that all APIs have a health endpoint, so to make it consistent I want to write it once and use it many.

My idea is to have the function in a class library and then reference it in my azure function project, but it doesn't seem to work. For each thing I have tried, only the functions in my azure function project appear.

Here is my shared function class, written in a separate referenced class library:

    public class HealthEndpointFunctions
    {
        [FunctionName("Health")]
        public async Task<IActionResult> Health(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "health")] HttpRequest req)
        {
            return new OkResult();
        }
    }

So far I have tried:

  1. Just adding a project reference hoping it would be identified but the function is not listed.
  2. Adding a function class to the azure function project that uses this as a base class
public class Function1 : HealthEndpointFunctions
    {
        [FunctionName("Function1")]
        public  async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
        {
            return new OkObjectResult("OKely dokely");
        }
    }

... in this case, only Function1 in the derived class is available

Feels to me that there must be some configuration in the azure function project to enable this, if indeed it is even possible.

Also welcome to suggested other approaches.

kidshaw
  • 3,423
  • 2
  • 16
  • 28

2 Answers2

6

If you add <FunctionsInDependencies>true</FunctionsInDependencies> to your functions csproj it will pull any functions found within any dependent assemblies.

The caveat is that the assembly has to be loaded in order for this to work. In the example below I'm using startup to make a no-op call which loads the assembly with the function in it.

https://github.com/lukesharmansmith/FuncFromDependency

0
  • One of the work arounds is to call the objects of Base Class in your Derived class i.e.. If your Solution looks like this

    Solution
    │   Base Project
    └───────BaseFunction.cs   
    │   Derived Project
    └───────DerivedFunction.cs
    
    

    in your DerivedFunction.cs you can use DerivedProject.DerivedFunction.Run()

STEPS :

  1. Create azure function using visual studio
  2. Add your class library reference
  3. Deploy your function on azure.
  4. No need to set-up any dependency injections while referencing the libraries in your function project (in startup.cs) but you can use DI based on your requirement.

REFERENCES :

  1. An example workaround is here in which the OP calls HTTP trigger function methods in a Timer Trigger function.
  2. Multiple Azure App Function in a project
  • Thanks for the suggestion but I am specifically looking at a way that doesn't require code in the derived project. The methods themselves are one liners but based on injected services. Currently, an item template is the closest solution to getting this how I want. – kidshaw Dec 29 '21 at 17:03