Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a reference to an Azure Function C# project?

With the help of the latest Azure SDK, I got the Azure Function tools For Visual Studio So that I can debug my Azure Function on premises - cool.

My azure function needs to use the code of a Business object dll

From the .csx script I can make a reference to the .dll with such a directive at the start

#r  "ServiceBusQueue.Shared.dll"

There is no "Add reference..." in an Azure function project. I can only add a Build dependency so that the business object dll is built first.

But when the dll code is updated there is no copying of the dll output to the bin of my azure function directory. Publish on the web is ok This is required in order for the latest business object code to be used in local debug session.

Because there is no tab for Prebuild events in an Azure function project, the solution I have found is to write a Prebuild event in the Function App project

.funproj file

<Target Name="BeforeBuild">
    <Message Text="Copying dlls into Azure Function ..." />
    <Exec Command="Call CopyDlls.cmd $(Configuration)" />
</Target>

Batch file :

rem %1 is the configuration, debug or release
echo %1
mkdir .\ServiceBusQueueTriggerCSharp\bin
copy ..\ServiceBusQueue.Shared\bin\%1\*.* .\ServiceBusQueueTriggerCSharp\bin
rem DOS subtlety : test than return code is equal or greater than 1
rem : make the compilation to fail
if errorlevel 1 exit /b 1

That way the dll is copied in the bin directory of the Azure Function project.

Is there a more integrated/cleaner way to do such a thing ?

like image 300
Emmanuel DURIN Avatar asked Mar 10 '23 00:03

Emmanuel DURIN


1 Answers

Unfortunately the best workaround for this is, as you've discovered, to use a MSBuild target in the funproj file.

Alternatively, you can add a post-build step to your class library. Go to Properties -> Build Events -> Post-build event command line and add something like xcopy /Y $(TargetPath) $(SolutionDir)FunctionApp1\bin.

However, the VS tooling team is working on adding reference functionality for a future release of the functions tooling. See https://github.com/Azure/Azure-Functions/issues/90.

like image 176
Matt Mason Avatar answered Mar 13 '23 06:03

Matt Mason