簡體   English   中英

連接並簽出方法

[英]Connect and check out methods

我有一種方法可以連接到tfs並檢出文件。 我必須將其分為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);

            }

我建議您不要將函數設為靜態,而是可以將變量僅存儲在類級別(如果它們是靜態的,則仍可以將它們存儲在類級別,但是至少這樣可以對壽命有一定的范圍:

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);
    }

我建議您使用此代碼,他會處理許多服務器和憑據之間的封裝和重構方面

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM