1

I am trying to make a base/abstract class that contains an Azure Function, which I can use from within a different project when it inherits from the base, but can't seem to get the functions from within the base class of the first project to appear.

(Currently using Azure Functions v3 in .Net Core 3.1 in VS2019)

Here's what I have tried...

Base class in project 1:

public class BaseClass
{
    [FunctionName("Function1")]
    public IActionResult Function1(
           [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
           ILogger log)
    {
           // Function 1 code in here
    }
}

New class in project 2:

public class NewClass : BaseClass
{
    [FunctionName("Function2")]
    public IActionResult Function2(
           [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
           ILogger log)
    {
           // Function 2 code in here
    }
}

So, when I run project 2 locally, I thought I should get Function1 and Function2 endpoints ready to be used, but I only get the endpoint for Function2 that is available.

If I run project 1 locally, I get the endpoint for Function 1.

Is this even possible in Azure Functions and, if so, how do I achieve this?

UPDATE - If I move the BaseClass from project 1 into project 2, I get what I want, namely Function 1 and Function 2 at the same time.

UPDATE 2 Repro steps:

  1. Create project 1: Create a new Azure Functions v3 targeting .Net 3.1.
  2. Add an Azure Function using an HTTP trigger.
  3. Run the project. (Basic/demo function 1 should run as expected.)
  4. Create project 2: add another new project in the same solution: Azure Functions v3 targeting .Net 3.1.
  5. Add an Azure Function using an HTTP trigger (to the new project).
  6. Run the new project. (Basic/demo function 2 should run as expected.)
  7. Add a project reference from Project 2 to Project 1.
  8. Make Project 2's class inherit from Project 1's class.
  9. Change each function in each project to NOT be a static class (to allow inheritance to work)
  10. Run Project 2 - only Project 2's functions are displayed.

...then...

Move the Function1.cs file from Project 1 into Project 2 and rename classes/namespaces appropriately and then run the project... both functions are displayed.

Brett Rigby
  • 6,101
  • 10
  • 46
  • 76
  • Apologies - the first draft of this question probably didn't cover all angles/elements, which is quite probably why I got downvoted. :-( Hope it makes more sense now. – Brett Rigby Dec 16 '21 at 08:06

2 Answers2

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()

but the whole point of using Azure functions is to have multiple functions in a single project that will all be deployed at the same time. So, whatever you are doing is expectable (i.e.. Adding The function from Project2 to project1 and using inheritance).

If you are trying to debug both the functions at the same time then you can Add Other Project and using the reference to the existing project but using different ports.

REFERENCES: Multiple Azure App Function in a project Multiple function projects using different ports

SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
0

I think what you are looking is the <FunctionsInDependencies>true</FunctionsInDependencies> property. More info:

Kiramm
  • 331
  • 3
  • 19