如何成功连接到TFS 2010工作项目商店?

时间:2012-02-06 02:20:34

标签: tfs tfs2010 tfs-workitem

我已经尝试了两种方法连接到我们正在运行的TFS服务器的workitemstore。尝试A是连接到配置服务器并使用GetService<WorkItemStore>()方法。这总是返回null。

尝试B是连接到TfsTeamProjectCollection并使用GetService<WorkItemStore>()方法或将项目集合传递给WorkItemStore构造函数。在尝试B时,我得到一个异常,说明“从调用COM组件返回错误HRESULT E_FAIL”。我可以找到的唯一信息似乎表明存在一些权限问题,但我已经确认我已经通过身份验证,对整个项目集合具有读访问权限,并通过VS 2011开发预览进行适当连接和插入。

以下是我如何联系...

    public TfsConfigurationServer GetConfigurationServer()
    {
        Uri tfsUri = new Uri(configs.TfsUri);
        TfsConfigurationServer server = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri, credProvider);
        server.Authenticate();
        if (server.HasAuthenticated == false)
            throw new InvalidOperationException("You can't authenticate against the tfs instance.");
        return server;
    }

    public TfsTeamProjectCollection GetProjectCollectionInstance(string projectCollectionName)
    {
        Uri tfsUri = new Uri(configs.TfsUri + "/" + projectCollectionName);         
        TfsTeamProjectCollection collection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri, credProvider);
        collection.Authenticate();
        if (collection.HasAuthenticated == false)
            throw new InvalidOperationException("You can't authenticate against the tfs instance.");
        return collection;
    }

这就是我如何尝试获取WorkItemStore(愚蠢的代码来说明问题)......

    public WorkItemProvider()
    {
        if (workItems == null)
            workItems = ServerProvider.ServerInstance.GetService<WorkItemStore>();
        if (workItems == null)
            workItems = ServerProvider.ProjectCollectionInstance.GetService<WorkItemStore>();
        if (workItems == null)
            workItems = new WorkItemStore(ServerProvider.ProjectCollectionInstance);
        if (workItems == null)
            throw new NullReferenceException("Couldn't load work item store.");
    }

我与服务器不在同一个域中,但是我作为具有ICredentialsProvider的域用户进行身份验证,并且我已经确认我已经过该用户的身份验证。任何指针都会有所帮助。

2 个答案:

答案 0 :(得分:2)

检查这是否符合您的要求:

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace GetsWorkItem
{
    class Program
    {
        static void Main()
        {
            TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://<TFS>:8080/tfs/<COLLECTION>"));
            WorkItemStore workItemStore= (WorkItemStore) teamProjectCollection.GetService(typeof (WorkItemStore));

            WorkItem workItem = workItemStore.GetWorkItem(1234);
        }
    }
}

答案 1 :(得分:0)

我相信this article或许可以回答你的问题。它说如果你以稍微不同的方式实例化你的WorkItemStore,你会得到一个不同的例外:

System.TypeInitializationException: The type initializer for ‘Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore’ threw an exception. —> System.IO.FileLoadException: Mixed mode assembly is built against version ‘v2.0.50727′ of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

修复是一个简单的web.config更改,添加以下内容:

<?xml version="1.0"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

希望这有帮助!当我遇到同样的错误时,为我工作。

相关问题