Introduction
When I started migration process to Azure Function 3.0 of an existing project, I have discovered small, but painful inconvenience. The problem was that all function.json files were permanently removed from output folder just before Azure Function Tools func.exe was started. I have to confess it was very annoying. There were suggestions e.g. on StackOverflow or other sites to run PowerShell which copies files later – after emulator is started, but it was not a solution I like.
When I press F5 button in my IDE I want to have Azure Functions up and ready to debug.
Solution
This is how I solved this problem. I discovered that files are removed by NuGet package Microsoft.NET.Sdk.Functions. I made an analysis what tasks are run in which order and I made my small solution to avoid this problem. I just added to csproj file custom target which is run after _GenerateFunctionsExtensionsMetadataPostBuild target. The set dependency is really important. Now Azure Functions works like a charm. When I press F5 I’m ready to test the application.
Below I’m including my target definition with important parts. I hope it will save you a few hours of searching for a solution.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="[3.0.2]" />
<!-- ignored file part -->
</ItemGroup>
<ItemGroup>
<!-- ignored file part -->
<None Update="folder1\function.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="folder2\function.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<!-- ignored file part -->
<Target Name="CopyFunctionBindingFilesToOutputDirectory" AfterTargets="_GenerateFunctionsExtensionsMetadataPostBuild">
<Copy SourceFiles="folder1\function.json" DestinationFiles="$(OutDir)folder1\function.json" />
<Copy SourceFiles="folder2\function.json" DestinationFiles="$(OutDir)folder2\function.json" />
</Target>
</Project>
NOTE:
- CopyFunctionBindingFilesToOutputDirectory – is just my target name
- _GenerateFunctionsExtensionsMetadataPostBuild – is a target name you must depend on to copy files after main output folder is cleaned up.