【问题标题】:Not able to connect to TFS and get a file from C#无法连接到 TFS 并从 C# 获取文件
【发布时间】:2018-12-24 21:25:15
【问题描述】:

这是我使用的代码。有些日子它工作正常,但突然停止工作。

        var TFS_UserName = "UserName";
        var TFS_Pass = "Password";
        var domain = "domain";
        var tfsUri = System.Configuration.ConfigurationManager.AppSettings["TFSUri"];
        Uri uri = new Uri(tfsUri);

        NetworkCredential cred = new NetworkCredential(TFS_UserName, TFS_Pass, domain);
        BasicAuthCredential basicCred = new BasicAuthCredential(cred);
        TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
        tfsCred.AllowInteractive = false;
        TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(uri, tfsCred);
        try
        {
            tpc.Authenticate();
        }
        catch (Exception ex)
        {
            return "User not Authorized" + ex.Message;
        }
        var vcServer = (VersionControlServer)tpc.GetService(typeof(VersionControlServer));
        var LocalPath = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["FileSave"]);
        var tempAddress = "File Path";
        var i = vcServer.GetItem(@"$/Folder/Path/" + tempAddress + "/" + "FileName");
        i.DownloadFile(LocalPath + "FileName");

这段代码突然开始在tpc.Authenticate();

处抛出异常

例外情况是: TF400324: 服务器 https://tfs/Address 不提供 Team Foundation 服务。 技术信息(针对管理员): 底层连接已关闭:发送时发生意外错误。

来源:Microsoft.TeamFoundation.Client

内部示例:底层连接已关闭:发送时发生意外错误。
{Inner Ex: Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。}

【问题讨论】:

  • 会不会只是网络问题?
  • 我也可以在浏览器和 Visual Studio 中登录到该 TFS 链接。在服务器中,这段代码也在工作,我们已经使用了一个多月。之后突然开始给出异常。
  • 我在开发gist.github.com/software-mariodiana/e2ab5eb9a9ed4ed441f2时从这个链接中获取了代码
  • 请参阅此线程进行故障排除:stackoverflow.com/questions/36109895/…,如果您安装了多个版本的客户端,请清除所有缓存,检查证书...希望有所帮助。
  • 你在连接TFS服务器时是否使用了代理?

标签: c# .net tfs


【解决方案1】:

可能是基本身份验证出了问题,尝试以这种方式连接:

var TFS_UserName = "UserName";
var TFS_Pass = "Password";
var domain = "domain";
var tfsUri = System.Configuration.ConfigurationManager.AppSettings["TFSUri"];
Uri uri = new Uri(tfsUri);

NetworkCredential cred = new NetworkCredential(UserName, Password);
TfsTeamProjectCollection tfsCollection = new TfsTeamProjectCollection(tfsUri, cred);
try
{
    tfsCollection.EnsureAuthenticated();
}
catch (Exception ex)
{
    return "User not Authorized" + ex.Message;
}

【讨论】:

  • 通过此更改也获得相同的异常 TF400324
  • 好的,请看这篇文章:ganshani.com/blog/2014/03/…
  • 阅读博客,但它告诉我解决方案只有在我做一个爱好项目时才有效。但它是一个团队项目。所以这对我的情况没有用。
【解决方案2】:

首先,您必须确保您可以在运行代码的机器上通过门户网站访问 TFS。

然后,尝试清理运行代码的计算机上的缓存文件夹。文件夹路径为:C:\Users\username\AppData\Local\Microsoft\Team Foundation\x.0\Cache

另外,请尝试删除Control Panel\All Control Panel Items\Credential Manager中与TFS相关的Windows凭据。

【讨论】:

  • 我也可以在浏览器和 Visual Studio 中登录到该 TFS 链接。在服务器中,这段代码也在工作,我们已经使用了一个多月。之后突然开始异常。您提供的解决方案对我不起作用。我试过这个..
  • 您是否在运行代码的同一台机器上的浏览器和 Visual Studio 中登录 TFS?
  • 是的.. 代码也在本地机器上运行。这两种方法我都试过了。
  • 您使用的是哪个版本的 TFS?
  • 我在本地使用 7.0
猜你喜欢
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
相关资源
最近更新 更多