Frederik Hansen
Frederik Hansen

Reputation: 506

Using AdWords API on Azure Functions

I'm trying to make an Azure Function that uses the AdWords API. 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.

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:

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)

Upvotes: 1

Views: 541

Answers (2)

Jerry Liu
Jerry Liu

Reputation: 17790

Update

The problem is now tracking here. Assemblies in runtimes folder [FunctionProject]\bin\Debug\netcoreapp2.1\bin\runtimes are not loaded into function context.

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.

Workaround is to copy those runtime assemblies to bin folder to make it loaded by function host. After Google.AdWords 24.1.0 installed, right click on project, Edit <FunctionProjectName>.csproj to add copy action. With full path to nuget package, we don't have to copy assembly to our project first.

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>

Upvotes: 1

Adrian
Adrian

Reputation: 11

The same workaround that Jerry describes will work for the WinHttpHandler problem. I have the following in my Azure Functions v2 project file:

<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.

Upvotes: 1

Related Questions