MrSpudtastic
MrSpudtastic

Reputation: 215

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

I am trying to write a PowerShell script, but I'm running into an error.

When my script gets to the line

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

I get the error:

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

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

Though my question is very similar to this question, I already know that the Microsoft.TeamFoundation.Client.dll file and its dependencies are in the GAC. The other question never clarifies that, and I think that might affect the answers I will get.

Before the line where the error occurs, I have a number of Add-Type statements to make sure the references I need are there. Among these statements is an Add-Type statement pointing to the Microsoft.TeamFoundation.Client.dll. I have verified that it's looking in the right location.

I've also included a try-catch statement that prints the loader exceptions if anything goes wrong there. Currently, the script is successfully making it through those statements without hitting the catch block.

Given that I know that the relevant dll is already in the GAC, what could cause this error, and how would I fix it?

Upvotes: 0

Views: 1665

Answers (2)

js2010
js2010

Reputation: 27418

I've never used this, but I believe this would work. At least I didn't get a type not found error.

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

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

Not that in PS 6, there's no GAC. so you'd have to put the full path to the dll for using assembly.

Upvotes: 0

starian chen-MSFT
starian chen-MSFT

Reputation: 33698

I’d suggest you to load assemblies from directory directly, for example:

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

You also could copy assemblies to a special folder (e.g. Lib folder in current project)

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

Upvotes: 1

Related Questions