【问题标题】:Unable to find [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory] even though dll is already in GAC即使 dll 已在 GAC 中,也无法找到 [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]
【发布时间】:2019-07-10 17:56:59
【问题描述】:

我正在尝试编写 PowerShell 脚本,但遇到了错误。

当我的脚本到达该行时

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

我得到错误:

找不到类型 [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 中,什么可能导致此错误,我将如何修复它?

【问题讨论】:

  • 在尝试引用类型之前您是否尝试过Add-Type ...?即使是在 GAC 中,我发现我经常需要 Add-Type 才能引用类型。
  • 我确实有。我什至在它周围添加了一个 try-catch 语句,以在 Add-Type 失败时打印加载程序异常,因此我确认这些语句没有失败。
  • 如何检查这些程序集在 GAC 中?您可以通过添加引用将这些程序集添加到控制台应用程序吗?
  • @starianchen-MSFT 在脚本的前面,我执行 [Reflection.Assembly]::LoadFrom($assemblyPath),然后执行 Add-Type -Path $assemblyPath。我还手动检查了这些路径是否已添加到 GAC,方法是转到目录并直观地确认在我运行脚本时确实添加了程序集。
  • 更好的方法是从项目的文件夹中加载程序集。

标签: powershell tfsbuild gac


【解决方案1】:

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

 $TfsAssembliesPath="C:\Program Files (x86)\Microsoft Visual Studio\2017\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"
...

【讨论】:

    【解决方案2】:

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

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

    不是说在 PS 6 中,没有 GAC。因此您必须将完整路径放入 dll 以使用程序集。

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多