erotavlas
erotavlas

Reputation: 4493

Unable to Add Type Microsoft.TeamFoundation.Client

I was trying to load the assembly so I could connect to TFS from powershell but I'm getting an error when I try to Add-Type. The nuget package was successfully downloaded and the file exists for the assembly I want to add.

My code is as follows

$sourceCodeDirectory = "C:\testing123";

CleanDirectory -directory $sourceCodeDirectory

[System.IO.Directory]::SetCurrentDirectory($sourceCodeDirectory);
$cwd = [System.IO.Directory]::GetCurrentDirectory();

Write-Output("CurrentWorkingDirectory: $cwd");

$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
$targetNugetExe = "$cwd\nuget.exe"
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
Set-Alias nuget $targetNugetExe -Scope Global -Verbose
nuget install Microsoft.TeamFoundationServer.Client -version '15.112.1' -OutputDirectory $cwd
nuget install Microsoft.TeamFoundationServer.ExtendedClient -version '15.112.1' -OutputDirectory $cwd
$uri = New-Object System.Uri -ArgumentList $TFSCollectionUri

try
{
    Write-Output 'Loading TFS Assemblies...'

    $assemblyPath = $cwd + "\Microsoft.TeamFoundationServer.ExtendedClient.15.112.1\lib\net45\Microsoft.TeamFoundation.Client.dll";
    Write-Output($assemblyPath);

    Add-Type -Path $assemblyPath
}
catch
{
    $_.LoaderExceptions 
    {
        Write-Error $_.Message
    }
}

With the following error

        Write-Error $_.Message

[ERROR] new-object : Could not load file or assembly 
[ERROR] 'Microsoft.VisualStudio.Services.Common, Version=15.0.0.0, Culture=neutral, 
[ERROR] PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot 
[ERROR] find the file specified.
[ERROR] At C:\Users\user\Documents\Visual Studio 2015\Projects\Build_FormsDesigne
[ERROR] r\Build_FormsDesigner\Build_FormsDesigner.ps1:107 char:23
[ERROR] + ... lProvider = new-object Microsoft.TeamFoundation.Client.UICredentialsP ...
[ERROR] +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : NotSpecified: (:) [New-Object], FileNotFoundExce 
[ERROR]    ption
[ERROR]     + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerS 
[ERROR]    hell.Commands.NewObjectCommand
[ERROR]  
[ERROR] Exception calling "GetTeamProjectCollection" with "2" argument(s): "Could not 
[ERROR] load file or assembly 'Microsoft.VisualStudio.Services.Common, 
[ERROR] Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of 
[ERROR] its dependencies. The system cannot find the file specified."
[ERROR] At C:\Users\user\Documents\Visual Studio 2015\Projects\Build_FormsDesigne
[ERROR] r\Build_FormsDesigner\Build_FormsDesigner.ps1:108 char:1
[ERROR] + $collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollecti ...
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
[ERROR]     + FullyQualifiedErrorId : FileNotFoundException
[ERROR]  
[ERROR] Method invocation failed because [System.String] does not contain a method 
[ERROR] named 'Authenticate'.
[ERROR] At C:\Users\user\Documents\Visual Studio 2015\Projects\Build_FormsDesigne
[ERROR] r\Build_FormsDesigner\Build_FormsDesigner.ps1:109 char:1
[ERROR] + $collection.Authenticate()
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
[ERROR]     + FullyQualifiedErrorId : MethodNotFound
[ERROR]  

Also when it errors out, something still has a lock on the DLL I was trying to load, preventing me from clearing the directory the next time I run the script. ( I have to close visual studio and re open it in order to remove the file lock)

EDIT: Just clarify, when I NuGet the following package

nuget install Microsoft.TeamFoundationServer.Client -version '15.112.1' -OutputDirectory $cwd

It pulls all the dependencies with it, so they exist in the folder as you can see below

enter image description here

Upvotes: 3

Views: 2514

Answers (3)

user3634685
user3634685

Reputation: 591

I had the same error when using power shell

Exception calling "GetConfigurationServer" with "1" argument(s): "Could not load file or assembly 'Microsoft.TeamFoundation.Common

and the fix is: replace the LoadWithPartialName with Add-Type -Path and worked

#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") | Out-Null
Add-Type -Path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Common.dll"

Adjust the path to the dlls.

Upvotes: 0

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31075

You need to add Microsoft.VisualStudio.Services.Common.dll too:

$assemblyPath1 = $cwd + "\Microsoft.VisualStudio.Services.Client.15.112.1\lib\net45\Microsoft.VisualStudio.Services.Common.dll";    

Add-Type -Path $assemblyPath1

Upvotes: 0

Rodrigo Werlang
Rodrigo Werlang

Reputation: 2176

Apparently you are getting this error because there are missing dependencies, as your exception is showing ...

Could not load file or assembly Microsoft.VisualStudio.Services.Common

From https://www.nuget.org/packages/Microsoft.TeamFoundationServer.Client/

Dependencies  
.NETFramework 4.5 
Microsoft.AspNet.WebApi.Client (>= 5.2.2)  
Microsoft.TeamFoundation.DistributedTask.Common (= 15.112.1)  
Microsoft.VisualStudio.Services.Client (= 15.112.1)  
Newtonsoft.Json (>= 8.0.3)

Upvotes: 2

Related Questions