【问题标题】:TFS WebHook fails after Webapi is published to IISWebapi 发布到 IIS 后 TFS WebHook 失败
【发布时间】:2017-08-31 14:25:00
【问题描述】:

我正在使用 TFS 2015 Webhook 在签入时收到通知。我使用 .NET 4.6 创建了一个 API,因此我可以接收通知。当我使用 Visual Studio 2015 加载 API 时它运行良好,但是一旦我将它发布到 IIS 7,TFS 开始显示状态代码:500,即使代码执行时没有任何异常。

这是来自 Web Api 的方法。我正在写一个输出到日志,以确保它被执行。每次都写“返回成功”。

    public HttpResponseMessage Post([FromBody]Models.Content value)
    {
        try
        {
            IndexUpdater iUpdater = new IndexUpdater();
            iUpdater.UpdateIndex(value.resource.changesetId);

            using (StreamWriter w = File.AppendText("C:\\publish\\TFSListenerAPI\\log.txt"))
            {
                w.WriteLine("returning success");
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch(Exception ex)
        {
            using (StreamWriter w = File.AppendText("C:\\publish\\TFSListenerAPI\\log.txt"))
            {
                w.WriteLine("erro");
                w.WriteLine(ex.Message);
            }
        }
    }

Here is the returned message

Here is the return when I execute the API using Visual Studio, same code.

编辑:刚刚发现错误500仅在执行以下方法时发生。奇怪的部分是代码有效,内容是 成功返回。

    private string GetFileContent(string tfsPath)
    {
        TfsTeamProjectCollection server = new TfsTeamProjectCollection(new Uri("http://tfs2015.com.br:8080/tfs/cd-jv"), new System.Net.NetworkCredential("user", "password"));
        server.Authenticate();
        VersionControlServer version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;
        Microsoft.TeamFoundation.VersionControl.Client.Item item = version.GetItem(tfsPath);
        string tempFileName = System.IO.Path.GetTempFileName();
        item.DownloadFile(tempFileName);
        var content = File.ReadAllText(tempFileName, Encoding.GetEncoding("ISO-8859-1"));
        return content;
    }

【问题讨论】:

  • GetFileContent 方法和 Webhook 句柄部分有什么关系?代码不足以让我们检查问题。
  • 很抱歉没有发布整个代码,我认为代码太多了。但基本上 UpdateIndex 方法会读取它接收到的 chageset 中的项目并将 TFS 路径传递给 GetFileContent。

标签: c# asp.net-web-api iis-7.5 tfs-2015 asp.net-webhooks


【解决方案1】:

所以,当我在发布后编辑帖子时,问题出在这一行:

TfsTeamProjectCollection server = new TfsTeamProjectCollection(new Uri("http://tfs2015.com.br:8080/tfs/cd-jv"), new System.Net.NetworkCredential("user", "password"));

由于某种原因,如果此请求是在 API 返回 OK 到 Webhook 之前发出的,Webhook 会显示错误 500。所以我发现使其工作的方法是确保仅在 tue API 返回后执行此行好的。可以通过使用 Async/Await 和 Thread.Sleep() 来完成。

【讨论】:

    【解决方案2】:

    根据错误消息Status Code: 500应该是内部服务器错误,发生了一些奇怪和不寻常的事情,可能根本不是你的错。

    当您在 Internet Information Services (IIS) 7.0 上遇到 500 错误时,请尝试检查事件查看器,是否有任何其他带有 HResult 代码的日志。然后参考下面的文章进行进一步的故障排除: HTTP Error 500.0 – Internal Server Error" error when you you open an IIS 7.0 Webpage

    同时检查这个帖子是否对你有帮助:How do I receive a custom webhook in an IIS hosted website?

    【讨论】:

    • 我在您提示后尝试检查事件查看器,但那里没有 IIS 日志条目。 :(
    猜你喜欢
    • 2014-02-14
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2023-03-08
    • 2018-07-06
    • 1970-01-01
    相关资源
    最近更新 更多