Within software development, it is common practice to create shared-code libraries. This helps you maintain your code better and reuse it in other places without the nasty copy-paste pattern. This can be done by simply adding a project reference or an artifact repository like NuGet. However, with Azure functions, this is a bit cumbersome and not well documented on how to achieve this.
Create default echo app
Start by creating a new Azure Function project with the default HTTP trigger. Be sure to set your *.csproj to:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
</ItemGroup>
Rename your Function1.cs to SharedEcho and make sure also to change the FunctionName attribute
public static class SharedEcho
{
[FunctionName("SharedEcho")]
public static async Task<IActionResult> Run(
Start your project, and the Azure SDK will discover the functions and create the corresponding folder structure in your */bin/* folder. The console will pop up, showing your function ready to be called.

Use it in another app
Now the first app is working, and we could package this in a NuGet package or just project reference it in a new Azure Function Project. For the sake of simplicity, we shall use the latter. Please create a new default Azure Function App as we did above; don’t rename the function. Add a project reference to the SharedEcho project, set FunctionApp1 as your default startup project. The FunctionApp1.csproj should look like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyEchoApp\MyEchoApp.csproj" />
</ItemGroup>
Start your function and see that the SDK does not discover your azure function in the referenced project.

By default, the Azure Functions SDK does not search in dependencies for existing functions, so the folder structure needed to run the function is not created and will not show up here at runtime.
Enable dependency discovery
To instruct the Azure Functions SDK to discover functions in referenced libraries, you need to add the <FunctionsInDependencies>true</FunctionsInDependencies>
SDK configuration.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<FunctionsInDependencies>true</FunctionsInDependencies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyEchoApp\MyEchoApp.csproj" />
</ItemGroup>
This should be enough for the SDK to search for additional functions. However, the .net optimizer will prevent the discovery by optimizing it away due to the referenced dll not being used. To force it and help set up a decent configuration, you would probably want to add an EchoAppConfiguration.cs to your EchoApp.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace MyEchoApp
{
public static class EchoAppConfiguration
{
public static IServiceCollection AddEchoApp(this IServiceCollection services, IConfigurationRoot configuration)
{
services.Replace(ServiceDescriptor.Singleton(typeof(IConfiguration), configuration));
return services;
}
}
}
Next, add a Startup.cs to your FunctionApp1. Calling the configuration on your EchoApp:
using MyEchoApp;
using FunctionApp1;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
[assembly: FunctionsStartup(typeof(Startup))]
namespace FunctionApp1
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
builder.Services.AddEchoApp(configuration);
}
}
}
Try to rerun your application and notice that the SDK did discover your shared functions, created the folder structure, and both your FunctionApp1 and the SharedEcho are shown in the console.
Conclusion
Yes, you can use precompiled azure functions in other applications. To do so, add <FunctionsInDependencies>true</FunctionsInDependencies> to your *.csproj. Ensure you use the shared assembly; otherwise, the optimizer will block it from discovery. Could use the configuration method shown in the example. If you want to see the full working demo, you can check out this repository.
Have fun! Erick
You must be logged in to post a comment.