Tuesday, October 15, 2013

ApplyAuthenticationToRequest errors

In the last months, just a few weeks after releasing 1.4.0-beta version of the Google APIs client library for .NET, I was asked by several developers about the following error:
System.TypeLoadException: Method 'ApplyAuthenticationToRequest' in type 'Google.Apis.Authentication.OAuth2.OAuth2Authenticator`1' from assembly 'Google.Apis.Authentication.OAuth2, Version=1.4.0.28223, Culture=neutral, PublicKeyToken=null' does not have an implementation.

This error also occurred after releasing 1.5.0-beta (Version=1.5.0.28991). The most problematic issue was that I didn't succeed in reproducing it on my machine and I spent a lot of time trying to figure out what is the exact problem ("But It works ON MY MACHINE!").

I understood then that the real problem wasn't our implementation of ApplyAuthenticationToRequest, but was based on the usage of System.Net assembly, in particular on the parameter to ApplyAuthenticationToRequest - System.Net.HttpWebRequest.
In addition, other developers also complained about the following compilation warnings:
warning CS1684: Reference to type 'System.Net.HttpWebRequest' claims it is defined in 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Net.dll', but it could not be found
warning CS1684: Reference to type 'System.Net.WebResponse' claims it is defined in 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Net.dll', but it could not be found
warning CS1684: Reference to type 'System.Net.WebRequest' claims it is defined in 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Net.dll', but it could not be found

So... I investigated and investigated and had several different directions - does it depend on the Windows platform (Vista, Windows 7 and Windows 8), what is the different in the GAC content, etc.

To make a long story short, just lately we found out the core of all evil.
Users who encounter this error should install KB2468871 patch for the .NET framework 4. This patch includes a new version of System.Net.dll (and other core assemblies as well) and it solves this issue.

Special thanks to the owners of Microsoft.Net.Http package which helped a lot in investigating this issue and refer me to this article, and specifically to the following section:
If you target the .NET Framework 4 or later, the computer must have the .NET Framework 4 with an update, Update 4.0.3 for the .NET Framework 4, or the .NET Framework 4.5 installed.

Thanks also to our developers for reporting the issue, for collecting all the data I asked them to, and for offering solutions.


Nahal Mishmar, April 2013, Israel

Eyal

4 comments:

  1. Howdy! Today I decided to update my project to include the Google.Apis.Drive.v2 Client Library. Prior to this I only had (basically) directory_v1 (1.5.0.16-beta) and all necessary dependencies from the time of download (Google.Apis 1.5.0-beta and anything else). After downloading via NuGet I have Google.Apis 1.6.0-beta and a new Google.Apis.Auth 1.6.0-beta. The project still compiles, but I now receive the first error you mentioned in this post.

    Unfortunately I am unable to install the KB2468871 - I'm running windows 8.1 (fully up to date) and the update 'does not apply'. I do have .NET 4.5 installed and it is, to my knowledge, up to date (via Windows updates). Any thoughts?

    ReplyDelete
  2. I never saw that coming... The error occurred for people who upgraded from 1.4.0-beta to 1.5.0-beta.

    Can you open a new issue in our issue tracker - https://code.google.com/p/google-api-dotnet-client/issues/entry, and we will take it from there.
    For now, mu suggestion is to use our new Google.Apis.Auth library which is already included in your NuGet references. You can read more about it in https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2.

    ReplyDelete
    Replies
    1. Thanks, I've gone ahead and created an issue. Moving forward should I be using Google.Apis.Auth instead of Google.Apis.Authentication.OAuth2?

      Delete
    2. Definitely! Just for future use, I'm attaching a link to the bug you just opened - https://code.google.com/p/google-api-dotnet-client/issues/detail?id=408.

      Delete