2

I'm looking into publishing a reusable Azure Function as a NuGet package, in order to easily consume it in other projects. This way I can reference the NuGet package in other projects. This way I could dynamically compose a set of Azure Functions to be deployed to an Azure Function service.

Is this currently possible? Or, can functions e.g. be defined in an external assembly, and be "picked up" by the Azure Function host?

I know this is possible with Azure WebJobs, but I haven't found a way to achieve the same result using Azure Functions.

Nsevens
  • 2,588
  • 1
  • 17
  • 34

2 Answers2

9

Adding <FunctionsInDependencies>true</FunctionsInDependencies> in the .csproj file seems to do the trick.

Do make sure you actually call the dependency somewhere in the code (e.g. in Startup.cs) to make sure the assembly isn't optimized away by the compiler.

Nsevens
  • 2,588
  • 1
  • 17
  • 34
  • The second part of this answer was a key part of the answer for me. Especially when starting a new project, this can be a pain to find. Thanks! – martennis Oct 26 '20 at 13:46
-2

Azure function host discovers function entry points in the same assembly of host (annotated by FunctionName in Run method). So you will only be able to externalize all the reusable codes to separate nugets, but would still need to define entry point for each of the function in the host. The function body might then be just bare minimum wrapper calling your reusable assets, but needs to be present in host. Would you mind to share a bit more about the scenario you are trying to solve?

EDIT: Thanks @Nsevens for trying this out. <FunctionsInDependencies>true</FunctionsInDependencies> in the .csproj would load functions from dependencies. There should be an explicit call of some method (may be just a noop one) of the dependency from the host to make it a hard dependency for the compiler. I did not see any official documentation about this.

krishg
  • 5,935
  • 2
  • 12
  • 19
  • Hi, thanks for the reply. We're developing a modular product-ish application, with dynamic implementations per customer. For example, we have Customer A which has an AutoDelete and AutoArchive function, and Customer B which only has AutoArchive. The way I described, we could create a customer solution and simply add _only_ the needed azure functions to their specific implementation – Nsevens Jul 02 '20 at 17:57
  • Thanks for explaining. Well, there seems to be a way, but kind of hacky (and there is of course no documentation or support for that :)). When you build a C# project containing function(s), it generates a folder per function in the build output (folder name is same as the function name). It contains function.json which describes the function for the runtime to understand and load. If you can somehow copy that into the target host app before deploying (like by some cmd or scripts) and then deploy the host app, it seems to work. But again as I said, it's bit of hacky solution. – krishg Jul 03 '20 at 14:02
  • Hi @KrishnenduGhosh-MSFT, I came across the `true` project property. This seems to make this scenario work. Is this a supported property? Or is this not supposed to be used that way? Would be nice to have some confirmation about this :) – Nsevens Jul 06 '20 at 08:39
  • 1
    FunctionsInDependencies is not documented in any published documentation. When I tried myself it does not work. I guess it might not have been released yet. I won't rely on it if it is scheduled to be a future feature unless officially announced somewhere. Would you mind sharing if you have any working sample with it. – krishg Jul 06 '20 at 10:25
  • I've created a demo application at https://github.com/NickSevens/demo-functionsindependencies , which shows a working Azure Function loaded from a dependency. – Nsevens Jul 08 '20 at 18:03
  • That's great. Thanks for exploring it. I guess it was not working for me since I was not doing an explicit dependency call from the host. – krishg Jul 08 '20 at 19:52
  • No problem. Any chance you could look into having this documented? – Nsevens Jul 09 '20 at 04:12
  • 1
    I will coordinate with the related document (https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-functions/functions-dotnet-class-library.md) authors and will get back. – krishg Jul 09 '20 at 06:00
  • @krishg Any updates on this one? I would also like to use this functionality but am a bit afraid that it will be pulled without warning if it stays undocumented. – Marcel Dutt Mar 28 '23 at 08:53