"Multiple ambiguous overloads" error when registering BatchExported event handler for TMX export

As mentioned recently elsewhere, I'm getting weird errors in my STraSAK when registering TMX export (and also import) event handler.
The same code runs just fine without errors with Studio 2014 and 2015, and I'm pretty sure it worked fine in earlier Studio 2017 versions (pre-SR1, like Studio 2017 CU5).

The code is PowerShell, but I'm pretty sure you get the idea since PS code looks very similar to C#...

This is the event handler (as PowerShell scriptblock). No rocket science, it just writes the number of processed and exported TUs to console:

# Event handler scriptblock
$OnBatchExported = {
    param([System.Object]$sender, [Sdl.LanguagePlatform.TranslationMemoryApi.BatchExportedEventArgs]$e)
    $TotalProcessed = $e.TotalProcessed
    $TotalExported = $e.TotalExported
    Write-Host "TUs processed: $TotalProcessed, exported: $TotalExported`r" -NoNewLine
}

And this is the relevant piece of the code - it's actually trivial, it gets a standard Studio TM object, creates TM exporter object, registers the event handler, runes the actual export and then unregisters the event handler. Again, no rocket science...

$TM = Get-FilebasedTM $SDLTM.FullName
$Exporter = New-Object Sdl.LanguagePlatform.TranslationMemoryApi.TranslationMemoryExporter ($TM.LanguageDirection)
$Exporter.Add_BatchExported($OnBatchExported)
$Exporter.Export("$TMXPath\$TMXName", ($Force.IsPresent))
$Exporter.Remove_BatchExported($OnBatchExported)

This code runs perfectly with Studio 2015, but fails with Studio 2017 (and also 2019) - see the video below.

The error 'Multiple ambiguous overloads found for "Add_BatchExported" and the argument count: "1"' would suggest that there is some difference in the API in Studio 2017/2019... yet, there is nothing mentioned in the relevant API documentation: producthelp.sdl.com/.../d829a063-705f-e34b-f504-a882125aed13.htm

BTW, the way the event handler is registered/unregistered is correct, see wensveen.wordpress.com/.../

EDIT:
Even the PrimalSense context help (which reads parameters directly from the DLLs) is not helpful since the event handler overloads are simply identical... That's really sick :(
How is one supposed to differentiate between these two?!?!

Screenshot showing an autocomplete suggestion for 'add_BatchExported' method with one parameter in Trados Studio.
Screenshot displaying a second autocomplete suggestion for 'add_BatchExported' method, indicating ambiguity in Trados Studio.

Play this video



Generated Image Alt-Text
[edited by: Trados AI at 1:18 PM (GMT 0) on 5 Mar 2024]
Parents
  • After many sleepless nights and hours of Googling I finally found a solution!
    As usually, it's very simple actually...

    Normally, Powershell creates the right delegate from the scriptblock ($OnBatchExported in my case) automatically behind the scenes, so no additional code is needed than what I originally had.
    And it worked just fine until SDL added the "duplicate" exporter/importer.

    All what's needed now is to explicitly convert the scriptblock to the right type before passing it to the add_BatchExported or remove_BatchExported methods:

    # Create BatchExported event handler type
    $BatchExportedEventHandlerType = [System.Type] "System.EventHandler[Sdl.LanguagePlatform.TranslationMemoryApi.BatchExportedEventArgs]"

    # BatchExported event handler scriptblock
    $OnBatchExported = {
        param([System.Object]$sender, [Sdl.LanguagePlatform.TranslationMemoryApi.BatchExportedEventArgs]$e)
        $TotalProcessed = $e.TotalProcessed
        $TotalExported = $e.TotalExported
        Write-Host "TUs processed: $TotalProcessed, exported: $TotalExported`r" -NoNewLine
    } -as $BatchExportedEventHandlerType

    (the -as $BatchExportedEventHandlerType on the last line does the converting trick)

    The rest of the code can stay as-is and Powershell can then correctly find non-ambiguous overloads... and it works fine in older AND newer Studio versions too.

Reply
  • After many sleepless nights and hours of Googling I finally found a solution!
    As usually, it's very simple actually...

    Normally, Powershell creates the right delegate from the scriptblock ($OnBatchExported in my case) automatically behind the scenes, so no additional code is needed than what I originally had.
    And it worked just fine until SDL added the "duplicate" exporter/importer.

    All what's needed now is to explicitly convert the scriptblock to the right type before passing it to the add_BatchExported or remove_BatchExported methods:

    # Create BatchExported event handler type
    $BatchExportedEventHandlerType = [System.Type] "System.EventHandler[Sdl.LanguagePlatform.TranslationMemoryApi.BatchExportedEventArgs]"

    # BatchExported event handler scriptblock
    $OnBatchExported = {
        param([System.Object]$sender, [Sdl.LanguagePlatform.TranslationMemoryApi.BatchExportedEventArgs]$e)
        $TotalProcessed = $e.TotalProcessed
        $TotalExported = $e.TotalExported
        Write-Host "TUs processed: $TotalProcessed, exported: $TotalExported`r" -NoNewLine
    } -as $BatchExportedEventHandlerType

    (the -as $BatchExportedEventHandlerType on the last line does the converting trick)

    The rest of the code can stay as-is and Powershell can then correctly find non-ambiguous overloads... and it works fine in older AND newer Studio versions too.

Children
No Data