1

我有一种连接到 tfs 并签出文件的方法。我必须将它分成 2 种方法,因为它们不会连续发生。但我不确定如何将其分成 2 种方法,因为如果我进行了结帐,这意味着我必须再次获取凭据和项目集合?

public static void Connect(String server, string path)
        {
            try
            {
                Uri serverUri = new Uri(server + "/tfs");
                ICredentialsProvider credentials = new UICredentialsProvider();
                TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(serverUri, credentials);
                tpc.EnsureAuthenticated();

                VersionControlServer versionControl = tpc.GetService<VersionControlServer>();

                Workspace workspace = versionControl.TryGetWorkspace(path);
                workspace.PendEdit(path);

            }
4

2 回答 2

1

我建议您不要将函数设为静态,然后您可以简单地将变量存储在类级别(如果它们是静态的,您仍然可以将它们存储在类级别,但至少这样您对生命周期有一定的了解:

public class TfsWrapper
{
    private TfsTeamProjectCollection tpc = null;
    private VersionControlServer versionControl = null;

    public TfsWrapper(string server, ...)
    {
        try
        {
            Uri serverUri = new Uri(server + "/tfs");
            ICredentialsProvider credentials = new UICredentialsProvider();
            tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(serverUri, credentials);
            tpc.EnsureAuthenticated();
            versionControl = tpc.GetService<VersionControlServer>();
        }
    }

    public void Checkout(string path)
    {
        Workspace workspace = versionControl.TryGetWorkspace(path);
        workspace.PendEdit(path);
    }
于 2013-11-15T14:40:14.023 回答
0

我建议您使用此代码,他处理大量服务器和凭据之间的封装和重构方面

链接:http: //blogs.msdn.com/b/buckh/archive/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer.aspx

于 2013-11-13T15:15:35.210 回答