2016-01-21 29 views
2

我試圖執行一個查詢我們的編譯結果TFS實例(最終這將在辦公室內所示的構建顯示器)的一個簡單的投票服務。下面的例子中here,我想出了以下嘗試:的NullReferenceException當輪詢Microsoft.VisualStudio.Services.Client爲構建

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 
+0

您是在查詢XAML構建還是基於任務的構建? –

+0

@DanielMann:基於任務的(I通過枚舉的另一成員是'DefinitionType.Xaml')。 –

+0

@DanielMann:我剛剛發現這個錯誤是在服務器端引起的(或者說,至少有太多......)。任何想法可能是什麼問題? –

回答

0

我發現的錯誤。簡答題,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());