1

I'm having trouble with adding the new Google Analytics APIs to an Excel add-in that I'm building using ExcelDna.

I've added these Nugets:

  <package id="Google.Apis" version="1.9.3" targetFramework="net45" />
  <package id="Google.Apis.Analytics.v3" version="1.9.2.1160" targetFramework="net45" />
  <package id="Google.Apis.Auth" version="1.9.3" targetFramework="net45" />
  <package id="Google.Apis.Core" version="1.9.3" targetFramework="net45" />

If I create a class:

public class Foo {

 public Foo() {
    AnalyticsService bar = null;
 }

}

As soon as I try to create an instance of this class I get a StackoverflowException.

So I'm guessing it's an issue with loading of the Google Apis assemblys?

Using FusionLog I can see that Google.Apis.Analytics.v3.dll loads ok but in Google.Apis.DLL there's a problem:

*** Assembly Binder Log Entry  (2015-08-27 @ 14:12:32) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Program Files\Microsoft Office\Office14\EXCEL.EXE
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = Google.Apis, Version=1.9.2.27817, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab
 (Fully-specified)
LOG: Appbase = file:///C:/Checkouts/NielsBosma/trunk/projects/SeoTools/bin/Debug
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = Excel-DNA: C:\Checkouts\NielsBosma\trunk\projects\SeoTools\bin\Debug\SeoTools64.xll
Calling assembly : Google.Apis.Analytics.v3, Version=1.9.2.116, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Google.Apis, Version=1.9.2.27817, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Checkouts/NielsBosma/trunk/projects/SeoTools/bin/Debug/Google.Apis.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Checkouts\NielsBosma\trunk\projects\SeoTools\bin\Debug\Google.Apis.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Google.Apis, Version=1.9.3.19379, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab
WRN: Comparing the assembly name resulted in the mismatch: Revision Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

I've created a console app with these packages where this problem doesn't exsist.

Now I'm stuck. What to do next?

UPDATE

I updated NetOffice and got rid of the StackoverflowExeception. Now I get this exception:

Could not load file or assembly 'Google.Apis, Version=1.9.2.27817, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Niels Bosma
  • 11,758
  • 29
  • 89
  • 148
  • Is there perhaps a binding redirect in the Console app that works? Maybe you can install the slightly older 1.9.2 packages for the other dependencies (using explicit -Version on the NuGet command line). – Govert Aug 28 '15 at 10:53
  • @govert Yes there's a bindingRedirect for "System.Net.Http.Primitives" added in the app.config by nuget that seems to be the issue. I reproduced the problem in a small solution with only ExcelDna and the Google Analytics Apis. The console verison of this works. Is the app.config stuff included in the xll? – Niels Bosma Aug 28 '15 at 12:39
  • 1
    You have to make your own MyAddin.xll.config file that sits next to the MyAddIn.xll file. I'm not sure if binding redirects from the .xll.config file will work. Does your console app give the same error if you remove the .config file? – Govert Aug 28 '15 at 14:03
  • Ok removing the Google api nuget and reading it again + MyAddin.xll.config fix solved it. Is there any way the config file can be included in the xll? – Niels Bosma Aug 28 '15 at 14:27
  • If you run ExcelDnaPack, then the .xll.config is automatically included in the packed .xll. If there's an actual file, then it is used instead of the packed one. – Govert Aug 28 '15 at 23:15
  • @NielsBosma Have you find a solution for this? I'm having a similar issue! Would please share if you got this done somehow! – curiousBoy Dec 18 '15 at 19:18

1 Answers1

0

the only thing you should have imported was

PM> Install-Package Google.Apis.Analytics.v3

I tested the code you posted and its not giving me an error.

 public class Foo
    {

        public Foo()
        {
            AnalyticsService bar = null;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Foo test = new Foo();
        }
    }

Oauth2 example with Google Analtyics

/// <summary>
        /// Authenticate to Google Using Oauth2
        /// Documentation https://developers.google.com/accounts/docs/OAuth2
        /// </summary>
        /// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
        /// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
        /// <param name="userName">A string used to identify a user.</param>
        /// <returns></returns>
        public static AnalyticsService AuthenticateOauth(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] { AnalyticsService.Scope.Analytics,  // view and manage your analytics data
                                             AnalyticsService.Scope.AnalyticsEdit,  // edit management actives
                                             AnalyticsService.Scope.AnalyticsManageUsers,   // manage users
                                             AnalyticsService.Scope.AnalyticsReadonly};     // View analytics data

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore("Daimto.GoogleAnalytics.Auth.Store")).Result;

                AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Analytics API Sample",
                });
                return service;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.InnerException);
                return null;

            }

        }

Code ripped from Google Analytics API Authentication with C# tutorial

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    "As soon as I try to create an instance of this class I get a StackoverflowException." I created the class and it didn't give me an error. Please post an example that shows the error happening. – Linda Lawton - DaImTo Aug 28 '15 at 09:14