简体   繁体   English

Webapi发布到IIS后,TFS WebHook失败

[英]TFS WebHook fails after Webapi is published to IIS

I'm using TFS 2015 Webhook to be notified when a checkin occurs. 我正在使用TFS 2015 Webhook在发生签入时收到通知。 I've created an API using .NET 4.6 so I can receive the notifications. 我已经使用.NET 4.6创建了API,因此我可以接收通知。 It works perfectly when I use Visual Studio 2015 to load the API, but once I publish it to IIS 7, TFS starts to show Status Code: 500, even if the code is executed without any exception. 当我使用Visual Studio 2015加载API时,它可以完美工作,但是一旦将其发布到IIS 7,即使代码执行无任何异常,TFS也会开始显示状态码:500。

Here is the method from the Web Api. 这是来自Web Api的方法。 I'm writing an output to log, to make sure it is executed. 我正在将输出写入日志,以确保已执行。 It writes "returning success" every time. 它每次都写着“回归成功”。

    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. 这是我使用Visual Studio执行API时返回的代码。

Edit: Just found out that the error 500 happens only when the below method is executed. 编辑:刚发现错误500仅在执行以下方法时发生。 The weird part is the fact that the code works, and the content is returned with success. 奇怪的是,代码可以正常工作,并且成功返回了内容。

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

So, as I edit the post after posting it, the problem is in this line: 因此,在发布后编辑帖子时,问题出在这一行:

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

For some reason, if this request is made before the API returns OK to the Webhook, the Webhook shows the error 500. So the way I found to make it work was by making sure this line is executed only after tue API returns OK. 由于某种原因,如果在API向Webhook返回OK之前发出了此请求,则Webhook会显示错误500。因此,我发现使其工作的方法是确保仅在tue API返回OK之后才执行此行。 It can be done by using Async/Await and Thread.Sleep(). 可以通过使用Async / Await和Thread.Sleep()来完成。

According to the error message Status Code: 500 it should be internal server error, something strange and unusual happened that was likely not your fault at all. 根据错误消息Status Code: 500它应该是服务器内部错误,发生了奇怪的异常事件,这很可能根本不是您的错。

Try to check the event viewer, if there are any other logs with HResult codes when you encounter 500 error on an Internet Information Services (IIS) 7.0. 当您在Internet Information Services(IIS)7.0上遇到500错误时,请尝试检查事件查看器,是否还有其他带有HResult代码的日志。 Then refer to below article for further trouble shooting: HTTP Error 500.0 – Internal Server Error" error when you you open an IIS 7.0 Webpage 然后,请参考下面的文章以进一步解决问题: 打开IIS 7.0网页时出现“ HTTP错误500.0 –内部服务器错误”错误

Also check if this thread helps for you : How do I receive a custom webhook in an IIS hosted website? 另外,请检查此线程是否对您有所帮助: 如何在IIS托管的网站中收到自定义的Webhook?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM