轮询Microsoft {VisualStudio.Services.Client进行构建

时间:2016-01-21 08:10:52

标签: c# tfs tfs2015

我正在尝试实现一个简单的轮询服务,该服务查询我们的TFS实例以获取构建结果(最终这将显示在办公室的构建监视器上)。在示例here之后,我想出了以下尝试:

var uri = new Uri("http://tfs:8080/tfs/defaultcollection");
var connection = new VssConnection(uri, new VssClientCredentials());
var client = connection.GetClient<BuildHttpClient>();

var builds = await client.GetBuildsAsync(
    project: "OurProject",
    maxBuildsPerDefinition: 1,
    type: DefinitionType.Build);

但是最后一个语句在客户端库的深处抛出了一个NullReferenceException(底部的完整堆栈跟踪)。

我尝试了以下内容,但它确实有效,但当然它并没有让我得到我正在寻找的信息:)

var client = connection.GetClient<WorkItemTrackingHttpClient>();
var workItems = await client.GetWorkItemsAsync(ids: new List<int> { 7000, 7005});

我是否从根本上误解了有关如何查询构建的内容?如何避免此异常?

更新:这不仅仅是我!

我刚发现这个错误不是在客户端生成的(仅);它也在服务器端(也是)!对以下网址的GET请求:

http://tfs:8080/tfs/defaultCollection/OurProject/_apis/build/builds?api-version=2.0&maxBuildsPerDefinition=1&type=build

产生500 Internal Server Error响应,其中包含以下内容:

{
  "$id": "1",
  "innerException": null,
  "message": "Object reference not set to an instance of an object.",
  "typeName": "System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
  "typeKey": "NullReferenceException",
  "errorCode": 0,
  "eventId": 0
}

鉴于此,我该如何排除故障?我应该询问/告诉运行我们的TFS服务器的运营人员?我们在寻找什么?

(客户端)异常的堆栈跟踪:

Unhandled Exception: System.AggregateException: One or more errors occurred. 
---> Microsoft.VisualStudio.Services.WebApi.VssServiceResponseException: Object reference not set to an instance of an object.
---> System.NullReferenceException: Object reference not set to an instance of an object.
   --- End of inner exception stack trace ---
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.HandleResponse(HttpResponseMessage response)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.SendAsync>d__49.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__47`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.SendAsync>d__53`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__52`1.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at ConsoleApplication1.Program.Work() in c:\users\tly01\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 32
   at ConsoleApplication1.Program.Main(String[] args) in c:\users\tly01\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 20

1 个答案:

答案 0 :(得分:0)

我发现了错误。简答,RTFM:)

事实证明maxBuildsPerDefinition参数仅在指定definitions时才有效。如果只给出前者,则服务器返回500。

(你可以说401 Bad Request会有更好的回应,但至少现在我知道怎么办了。)

我的解决方法是在客户端进行过滤;我获得所有项目的所有构建,然后按定义分组并选择最新的项目:

var builds = await client
    .GetBuildsAsync(
        project: "OurProject",
        type: DefinitionType.Build).Result
    .GroupBy(build => build.Definition.Id)
    .Select(g => g
        .OrderByDescending(build.FinishTime ?? build.StartTime ?? DateTime.MinValue)
        .First());