我正在尝试实现一个简单的轮询服务,该服务将查询我们的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
最佳答案
我发现了错误。简短的回答,RTFM :)
事实证明,仅当还指定了maxBuildsPerDefinition
时,definitions
参数才有效。如果仅给出前者,则服务器返回500。
(您可能会说“ 401错误的请求”会是更好的响应,但至少现在我知道该怎么做了。)
我的解决方法是在客户端进行过滤;我得到所有项目的所有构建,然后按定义分组并选择最新的一个:
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());
关于c# - 轮询Microsoft.VisualStudio.Services.Client的版本时发生NullReferenceException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34918426/