【问题标题】:Unable to Add Type Microsoft.TeamFoundation.Client无法添加类型 Microsoft.TeamFoundation.Client
【发布时间】:2018-08-28 10:37:48
【问题描述】:

我试图加载程序集,以便可以从 powershell 连接到 TFS,但是当我尝试添加类型时出现错误。 nuget 包已成功下载,并且我要添加的程序集的文件存在。

我的代码如下

$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
    }
}

出现以下错误

        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]  

此外,当它出错时,我试图加载的 DLL 上仍有一些东西被锁定,从而阻止我在下次运行脚本时清除目录。 (我必须关闭 Visual Studio 并重新打开它才能解除文件锁定)

编辑:澄清一下,当我 NuGet 以下包时

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

它会拉取所有依赖项,因此它们存在于文件夹中,如下所示

【问题讨论】:

  • 当你使用ForEach-Object时,你不能把脚本块放在自己的行上。
  • @TheIncorrigible1 抱歉,我不得不重新运行它...摆脱了 | % in the catch....我认为 foreach-object 指的是那个....我更新了错误消息

标签: powershell tfs


【解决方案1】:

显然您收到此错误是因为缺少依赖项,因为您的异常显示...

无法加载文件或程序集 Microsoft.VisualStudio.Services.Common

来自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)

【讨论】:

【解决方案2】:

您还需要添加Microsoft.VisualStudio.Services.Common.dll

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

Add-Type -Path $assemblyPath1

【讨论】:

  • Add-Type 会锁定 DLL,因此我无法在完成后清理目录。当脚本完成时,你知道如何释放它的锁吗?
  • 请使用嵌套shell检查这种情况下的解决方案:stackoverflow.com/questions/3369662/…,希望对您有所帮助。
【解决方案3】:

我在使用 power shell 时遇到了同样的错误

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

修复方法是: 用 Add-Type -Path 替换 LoadWithPartialName 并工作

#[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"

调整 dll 的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2013-10-09
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多