TFS WebHook fails after Webapi is published to IIS

667 views Asked by At

I'm using TFS 2015 Webhook to be notified when a checkin occurs. I've created an API using .NET 4.6 so I can receive the notifications. 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.

Here is the method from the 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.

Edit: Just found out that the error 500 happens only when the below method is executed. 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;
    }
2

There are 2 answers

0
Jeff Klein On BEST ANSWER

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. It can be done by using Async/Await and Thread.Sleep().

1
Andy Li-MSFT On

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.

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. 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

Also check if this thread helps for you : How do I receive a custom webhook in an IIS hosted website?