简体   繁体   English

在Azure Functions上使用AdWords API

[英]Using AdWords API on Azure Functions

I'm trying to make an Azure Function that uses the AdWords API. 我正在尝试制作一个使用AdWords API的Azure函数。 I'm at a point where my code is working fine, and all I have to do is make the function run at a given interval. 在我的代码可以正常工作的时候,我要做的就是使函数以给定的间隔运行。 However, when I try to run the function locally I get the following error message: 但是,当我尝试在本地运行该函数时,出现以下错误消息:

[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:\Users\frede\AppData\Local\Azure.Functions.V2.Cli\func.dll.config line 204)

If I try to upload the function and run it I get this error: 如果我尝试上载该功能并运行它,则会出现此错误:

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)

I'm using the .NET Standard 2.0. 我正在使用.NET Standard 2.0。

What can I do to fix this error and get my function running? 我该如何解决此错误并使我的功能运行?

UPDATE: 更新:

After solving the issue with help from Jerry Liu, I get the following error: 在Jerry Liu的帮助下解决了问题之后,出现以下错误:

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)

Update 更新

The problem is now tracking here . 现在在这里跟踪问题。 Assemblies in runtimes folder [FunctionProject]\\bin\\Debug\\netcoreapp2.1\\bin\\runtimes are not loaded into function context. 运行时文件夹[FunctionProject]\\bin\\Debug\\netcoreapp2.1\\bin\\runtimes中的程序集未加载到函数上下文中。

With the help of @Adrian, it turns out the problem WinHttpHandler is only supported on .NET Framework and .NET Core runtimes on Windows results from wrong assembly copied from lib %USERPROFILE%\\.nuget\\packages\\system.net.http.winhttphandler\\4.4.0\\lib\\netstandard2.0 while we need runtime assembly. 在@Adrian的帮助下,发现问题WinHttpHandler is only supported on .NET Framework and .NET Core runtimes on Windows从lib %USERPROFILE%\\.nuget\\packages\\system.net.http.winhttphandler\\4.4.0\\lib\\netstandard2.0复制的错误程序集导致的结果WinHttpHandler is only supported on .NET Framework and .NET Core runtimes on Windows %USERPROFILE%\\.nuget\\packages\\system.net.http.winhttphandler\\4.4.0\\lib\\netstandard2.0而我们需要运行时汇编。

Workaround is to copy those runtime assemblies to bin folder to make it loaded by function host. 解决方法是将这些运行时程序集复制到bin文件夹,以使其由功能主机加载。 After Google.AdWords 24.1.0 installed, right click on project, Edit <FunctionProjectName>.csproj to add copy action. 安装Google.AdWords 24.1.0后,右键单击项目, Edit <FunctionProjectName>.csproj以添加复制操作。 With full path to nuget package, we don't have to copy assembly to our project first. 有了nuget包的完整路径,我们不必首先将程序集复制到我们的项目中。

Note that once we install packages of new version, these paths could change. 请注意,一旦我们安装了新版本的软件包,这些路径可能会更改。

  <!-- For publish -->
  <ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\system.private.servicemodel\4.4.2\runtimes\win7\lib\netstandard2.0\System.Private.ServiceModel.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Include="$(USERPROFILE)\.nuget\packages\system.net.http.winhttphandler\4.4.0\runtimes\win\lib\netstandard2.0\System.Net.Http.WinHttpHandler.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <!-- For local debug -->
  <Target Name="CopyRuntime" BeforeTargets="Build">
    <Copy SourceFiles="$(USERPROFILE)\.nuget\packages\system.net.http.winhttphandler\4.4.0\runtimes\win\lib\netstandard2.0\System.Net.Http.WinHttpHandler.dll" DestinationFolder="$(OutputPath)\bin" />
    <Copy SourceFiles="$(USERPROFILE)\.nuget\packages\system.private.servicemodel\4.4.2\runtimes\win7\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)\bin" />
  </Target>

The same workaround that Jerry describes will work for the WinHttpHandler problem. 杰里描述的相同解决方法将适用于WinHttpHandler问题。 I have the following in my Azure Functions v2 project file: 我的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, the WinHttpHandler addition only seems to be required if you are trying to make service requests to the Google Ads API - report requests and response work ok without it. FWIW,仅当您尝试向Google Ads API提出服务请求时才需要添加WinHttpHandler报告请求和响应可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM