在 Azure Functions 上使用 AdWords API



我正在尝试制作一个使用 AdWords API 的 Azure 函数。我正处于代码运行良好的地步,我所要做的就是让函数以给定的间隔运行。 但是,当我尝试在本地运行该函数时,我收到以下错误消息:

[22-Jun-18 19:08:58] Executed 'KeywordPlanner' (Failed, Id=ab7c3ecf-1238-4370-ab5b-17d547d354b3)
[22-Jun-18 19:08:58] System.Private.CoreLib: Exception while executing function: KeywordPlanner. System.Configuration.ConfigurationManager: Configuration system failed to initialize. System.Configuration.ConfigurationManager: Unrecognized configuration section system.serviceModel. (C:UsersfredeAppDataLocalAzure.Functions.V2.Clifunc.dll.config line 204)

如果我尝试上传函数并运行它,则会出现此错误:

2018-06-22T18:50:35  Welcome, you are now connected to log-streaming service.
2018-06-22T18:50:40.967 [Information] Executing 'KeywordPlanner' (Reason='This function was programmatically called via the host APIs.', Id=321756f7-3cf1-4235-a741-daf97d304058)
2018-06-22T18:50:40.972 [Error] System.Private.CoreLib: Exception while executing function: KeywordPlanner. Google.AdWords: Could not load file or assembly 'System.Private.ServiceModel, Version=4.1.2.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.
2018-06-22T18:50:41.039 [Error] Executed 'KeywordPlanner' (Failed, Id=321756f7-3cf1-4235-a741-daf97d304058)

我使用的是.NET Standard 2.0。

我该怎么做才能修复此错误并使我的函数运行?

更新:

在刘杰瑞的帮助下解决问题后,我收到以下错误:

2018-06-25T07:43:20.517 [Error] System.Private.CoreLib: Exception while executing function: KeywordPlanner. System.Net.Http.WinHttpHandler: WinHttpHandler is only supported on .NET Framework and .NET Core runtimes on Windows. It is not supported for Windows Store Applications (UWP) or Unix platforms.
2018-06-25T07:43:20.679 [Error] Executed 'KeywordPlanner' (Failed, Id=4231f32f-f83f-4f37-99a7-b90484933e2f)

更新

问题现在在这里跟踪。运行时文件夹[FunctionProject]binDebugnetcoreapp2.1binruntimes中的程序集不会加载到函数上下文中。

在@Adrian的帮助下,事实证明问题WinHttpHandler is only supported on .NET Framework and .NET Core runtimes on Windows从lib%USERPROFILE%.nugetpackagessystem.net.http.winhttphandler4.4.0libnetstandard2.0复制的错误程序集导致,而我们需要运行时汇编。

解决方法是将这些运行时程序集复制到bin文件夹,使其由函数宿主加载。安装Google.AdWords24.1.0 后,右键单击项目,Edit <FunctionProjectName>.csproj添加复制操作。使用 nuget 包的完整路径,我们不必先将程序集复制到项目中。

请注意,一旦我们安装了新版本的软件包,这些路径可能会更改。

<!-- For publish -->
<ItemGroup>
<None Include="$(USERPROFILE).nugetpackagessystem.private.servicemodel4.4.2runtimeswin7libnetstandard2.0System.Private.ServiceModel.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="$(USERPROFILE).nugetpackagessystem.net.http.winhttphandler4.4.0runtimeswinlibnetstandard2.0System.Net.Http.WinHttpHandler.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<!-- For local debug -->
<Target Name="CopyRuntime" BeforeTargets="Build">
<Copy SourceFiles="$(USERPROFILE).nugetpackagessystem.net.http.winhttphandler4.4.0runtimeswinlibnetstandard2.0System.Net.Http.WinHttpHandler.dll" DestinationFolder="$(OutputPath)bin" />
<Copy SourceFiles="$(USERPROFILE).nugetpackagessystem.private.servicemodel4.4.2runtimeswin7libnetstandard2.0System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)bin" />
</Target>

Jerry 描述的相同解决方法适用于WinHttpHandler问题。我的 Azure Functions v2 项目文件中有以下内容:

<ItemGroup>
<None Update="System.Private.ServiceModel.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="System.Net.Http.WinHttpHandler.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="CopySPSM" BeforeTargets="Build">
<Copy SourceFiles="System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)bin" />
<Copy SourceFiles="System.Net.Http.WinHttpHandler.dll" DestinationFolder="$(OutputPath)bin" />
</Target>

FWIW,仅当您尝试向 Google Ads API 发出服务请求时,才需要添加WinHttpHandler- 报告请求和响应在没有它的情况下工作正常。

最新更新