简体   繁体   中英

What causes a TargetInvocationException when creating an AdWords API service?

I'm learning a .Net library to connect to the AdWords API. My first block occurs when I try to create a service using my AdWordsUser object. I get a TargetInvocationException :

Exception has been thrown by the target of an invocation

Inner exception:

The value of the property 'type' cannot be parsed. The error is: Could not
load file or assembly 'Google.Ads.Common, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=52807268f2b614dc' or one of its dependencies. The located
assembly's manifest definition does not match the assembly reference. (Exception
from HRESULT: 0x80131040)

Based on my Googling, the main fix suggested is to add a reference to SoapListenerExtension in your app.config. I've tried two methods (shown at the end of the question) with no change.

My app includes references to the Google.Ads.Common , Google.AdWords , and System.Web.Services libraries. Here's my code, with expanded references to eliminate ambiguity.

var  headers    = new Dictionary<string, string>();
headers.Add("email", Properties.Settings.Default.AdWordsUserName);
// Rest of the creds
var  _user      = new Google.Api.Ads.AdWords.Lib.AdWordsUser(headers);
var  sig        = Google.Api.Ads.AdWords.Lib.AdWordsService.v201109.CampaignService;
var  rawService = _user.GetService(sig);  // The exception is thrown here
var  service    = (Google.Api.Ads.AdWords.v201109.CampaignService)rawService;

There's no change if I leave out the password, so I don't think it's a problem with my creds: the code hasn't gotten that far.

The Google.AdWords library includes some classes in the Google.Api.Ads.Common.Lib namespace, but not SoapListenerExtension . Is this tucked away in some other DLL in this library, perhaps?

I've looked through a dozen questions here on SO that relate to the AdWords API, but nothing relating to this specific error.


Edit

Using AppDomain.CurrentDomain.GetAssemblies() , I've polled the assemblies loaded. If I include this harmless line:

var  cs = Google.Api.Ads.AdWords.Lib.AdWordsService.v201109.CampaignService;

This assembly is loaded, seemingly successfully:

Loaded: Google.Ads.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

...so it looks like the assembly I have is unsigned. Sure enough, there is a signed version of the API available. Maddeningly, it's structured differently from the unsigned version, and most of the fully-qualified names aren't valid any more. Back to the drawing board.


Appendix

Web service method 1:

  <system.web>
    <webServices>
      <soapExtensionTypes>
        <add type="Google.Api.Ads.Common.Lib.SoapListenerExtension, Google.Ads.Common" priority="1" group="0"/>
      </soapExtensionTypes>
    </webServices>
  </system.web>

Method 2:

<system.web>
    <webServices>
        <soapExtensionTypes>
            <add type="Google.Api.Ads.Common.Lib.SoapListenerExtension, Google.Ads.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=52807268f2b614dc" priority="1" group="Low"/>
        </soapExtensionTypes>
    </webServices>
</system.web>

Swapping my references to the signed version did the trick. In retrospect, I suppose the error messages were pointing to that fairly clearly. I'm left wondering what the unsigned versions are used for or, putting it the other way, why is it that only the signed versions worked in this instance?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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