无法找到 [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory],即使 dll 已在 GAC 中

Unable to find [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory] even though dll is already in GAC

我正在尝试编写 PowerShell 脚本,但 运行 出错了。

当我的脚本到达行时

$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($server)

我收到错误:

Unable to find type [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]...

InvalidOperation: (Microsoft.TeamF...onServerFactory:TypeName) [], RuntimeException

虽然我的问题与this question非常相似,但我已经知道Microsoft.TeamFoundation.Client.dll文件及其依赖项在GAC中。另一个问题从来没有澄清这一点,我认为这可能会影响我得到的答案。

在发生错误的那一行之前,我有一些Add-Type语句来确保我需要的引用在那里。在这些语句中有一个指向 Microsoft.TeamFoundation.Client.dll 的 Add-Type 语句。我已经确认它正在寻找正确的位置。

我还包含了一个 try-catch 语句,如果那里出现任何问题,它会打印加载程序异常。目前,脚本已成功通过这些语句,而没有遇到 catch 块。

鉴于我知道相关的 dll 已经在 GAC 中,什么可能导致此错误,我该如何解决?

我建议你直接从目录加载程序集,例如:

 $TfsAssembliesPath="C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
    Add-Type -Path "$TfsAssembliesPath\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.ServiceBus.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.Client.Interactive.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Core.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.TestManagement.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.ProjectManagement.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Build.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Build.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Git.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.SourceControl.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.QueryLanguage.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.DataStoreLoader.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Proxy.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.TestManagement.Client.dll"
    Function CreateWorkItem{

    [string]$tfsCollectionUrl="TFS collection URL"
    [string]$tfsTeamProjectName="team project"

    $teamProjectCollection=[Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)
    $ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
    $proj = $ws.Projects[$tfsTeamProjectName]

    $wit = $proj.WorkItemTypes["Task"]

    #Create a new work item of that type
    $workitem = $wit.NewWorkItem()

    $workItem.Title = "Sample Task Title 3"
    $workItem.Description = "Sample Description"
    $workitem.AreaPath = $tfsTeamProjectName
    $workitem.IterationPath = $tfsTeamProjectName

    $workItem.Save()
    Write-Host "The TFS work item number is: " $workItem.Id
    }

您也可以将程序集复制到一个特殊文件夹(例如当前项目中的 Lib 文件夹)

    $TfsAssembliesPath="$PSScriptRoot\Libs"
    Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Client.dll"
    Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Common.dll"
...

我从未使用过它,但我相信它会起作用。至少我没有收到类型未找到错误。

using assembly Microsoft.TeamFoundation.Client
using namespace Microsoft.TeamFoundation.Client

$tfs = [TeamFoundationServerFactory]::GetServer($server)

不是 PS 6,没有 GAC。所以你必须把完整的路径放到dll中才能使用程序集。