【问题标题】:How to add a reference to an Azure Function C# project?如何添加对 Azure Function C# 项目的引用?
【发布时间】:2017-06-18 13:30:25
【问题描述】:

借助最新的 Azure SDK,我得到了 Visual Studio 的 Azure Function tools 这样我就可以在本地调试我的 Azure Function - 很酷。

我的天蓝色函数需要使用业务对象dll的代码

从 .csx 脚本中,我可以在开始时使用这样的指令引用 .dll

#r  "ServiceBusQueue.Shared.dll"

Azure 函数项目中没有“添加引用...”。 我只能添加一个 Build 依赖,以便首先构建业务对象 dll。

但是当更新 dll 代码时,没有将 dll 输出复制到我的 azure 函数目录的 bin 中。 在网络上发布是可以的 为了在本地调试会话中使用最新的业务对象代码,这是必需的。

因为 Azure 函数项目中没有 Prebuild 事件选项卡, 我找到的解决方案是在 Function App 项目中写一个Prebuild event

.funproj 文件

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

批处理文件:

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

这样 dll 就会被复制到 Azure Function 项目的 bin 目录中。

有没有更集成/更清洁的方法来做这样的事情?

【问题讨论】:

    标签: c# azure msbuild csproj azure-functions


    【解决方案1】:

    不幸的是,正如您所发现的,最好的解决方法是在 funproj 文件中使用 MSBuild 目标。

    或者,您可以将构建后步骤添加到您的类库中。转到 Properties -> Build Events -> Post-build event command line 并添加类似xcopy /Y $(TargetPath) $(SolutionDir)FunctionApp1\bin 的内容。

    不过,VS 工具团队正在努力为函数工具的未来版本添加参考功能。见https://github.com/Azure/Azure-Functions/issues/90

    【讨论】:

    • 感谢您的支持。急切等待添加参考功能
    • @EmmanuelDURIN 我编辑了 Matt 的帖子,解决方法稍微简单一些。如果帖子对您有帮助,请接受它作为答案:)
    【解决方案2】:

    我知道这是一个老问题,但我刚刚在 VS 2019 中遇到了这个问题,所以对于任何前来寻找的人:您可以在引用项目的属性中将“复制本地”设置为“是”。

    在解决方案资源管理器中,打开 Dependencies |您的 Functions 项目下的 Projects 节点,右键单击引用的项目,选择 Properties,然后将“Copy Local”设置为 Yes。这会将引用项目的 DLL 复制到 Functions 项目的输出目录,从而使该 DLL 包含在部署的包中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多