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

[亡魂溺海] 提交于 2019-12-07 12:23:02

问题


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 ?


回答1:


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.




回答2:


I know this is an old question, but I just ran into this issue with VS 2019, so for anyone else who comes looking: you can set "Copy Local" to Yes in the referenced project's properties.

In Solution Explorer, open the Dependencies | Projects node under your Functions project, right click on the referenced project, select Properties, and set "Copy Local" to Yes. That copies over the referenced project's DLL to your Functions project's output directory, which causes that DLL to be included in the package that is deployed.



来源:https://stackoverflow.com/questions/41985925/how-to-add-a-reference-to-an-azure-function-c-sharp-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!