Search code examples
c#tfstfs-sdktfs-2015

Is it possible to use TFS 2015 api to QueryHistory changesets from TFS 2010?


I'm performing a migration of TFS 2010 team projects to TFS 2015 by using the TFS API. I'm running into an issue when attempting to call VersionControlServer.GetLatestChangesetId() and VersionControlServer.QueryHistory(...).

I am using the nuget Team Foundation packages for 2015 to provide dlls.

The exception I am getting is:

'VersionControlServer.GetLatestChangesetId()' threw an exception of type 'System.Xml.XmlException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146232000
    HelpLink: null
    InnerException: null
    LineNumber: 0
    LinePosition: 0
    Message: "Unexpected end of file."
    Source: "System.Runtime.Serialization"
    SourceUri: null
    StackTrace: "   at System.Xml.EncodingStreamWrapper.ReadBOMEncoding(Boolean notOutOfBand)\r\n   at System.Xml.EncodingStreamWrapper..ctor(Stream stream, Encoding encoding)\r\n   at System.Xml.XmlUTF8TextReader.SetInput(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)\r\n   at Microsoft.TeamFoundation.Client.Channels.TfsSoapMessageEncoder.ReadMessage(Stream stream)\r\n   at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.ReadMessage(HttpWebResponse webResponse, WebException webException, Stream responseStream, Boolean& closeResponse)\r\n   at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.ReadResponse(HttpWebResponse webResponse, WebException webException)\r\n   at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.IsAuthenticationChallenge(TfsMessage requestMessage, HttpWebResponse webResponse, WebException webException, TfsMessage& responseMessage)\r\n   at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendR
equest()\r\n   at Microsoft.TeamFoundation.Client.Channels.TfsHttpRequestChannel.Request(TfsMessage message, TimeSpan timeout)\r\n   at Microsoft.TeamFoundation.Client.Channels.TfsHttpClientBase.Invoke(TfsClientOperation operation, Object[] parameters, TimeSpan timeout, Object[]& outputs)\r\n   at Microsoft.TeamFoundation.VersionControl.Client.Repository.GetRepositoryProperties()\r\n   at Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer.GetLatestChangesetId()"
    TargetSite: {SupportedEncoding ReadBOMEncoding(Boolean)}

The exception is basically identical for QueryHistory and GetLatestChangesetId. From the stacktrace it looks like TFS may be handling encoding differently between from 2010 to 2015.

I am looking for a solution that will allow me to query both the legacy TFS 2010 and our new TFS 2015 via the API.


Solution

  • I can use the API to query from TFS2010 and TFS2015, following is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;
    
    namespace TFSAPI
    {
        class Program
        {
            static void Main(string[] args)
            {
                string project = "http://xxxxx:8080/tfs/DefaultCollection";
                NetworkCredential nc = new NetworkCredential("username","pwd");
                TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(project), nc);
                tpc.Authenticate();
                VersionControlServer vcs = tpc.GetService<VersionControlServer>();
                int cid = vcs.GetLatestChangesetId();
                string p = "$/ProjectName";
                var h = vcs.QueryHistory(p,RecursionType.Full,5);
                Console.WriteLine("Following are the latest 5 changeset in " + p +":");
                foreach (Changeset item in h)
                {
                    Console.WriteLine("{0} {1}", item.ChangesetId, item.Comment);
                }
                Console.WriteLine("The latest changeset ID is:" + cid);
                Console.ReadLine();
            }
        }
    }