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 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;
}
So, as I edit the post after posting it, the problem is in this line:
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().