1

使用此处.NET 工具包页面中的创建缺陷示例。

尝试使用 DynamicJsonObject 时出现以下错误 -

toCreate["Name"] 'toCreate["Name"]' 抛出类型为 'System.Collections.Generic.KeyNotFoundException' 动态 {System.Collections.Generic.KeyNotFoundException} 的异常

我正在使用的代码正如示例所述:

 DynamicJsonObject toCreate = new DynamicJsonObject();
        toCreate["Name"] = "My Defect";

        CreateResult createResult = restApi.Create(workspaceRef, "defect", toCreate);

这是我的问题还是 API 有缺陷?

4

1 回答 1

1

以下是有效的完整示例。当我们创建缺陷时:

CreateResult createResult = restApi.Create(workspaceRef, "defect", myDefect);

并立即尝试打印其 FormattedID:

Console.WriteLine(myDefect["FormattedID"]);

生成相同的 System.Collections.Generic.KeyNotFoundException。你的代码中可能有类似的东西吗?

另一方面,如果我们创建一个缺陷,然后通过引用获取,我们可以打印 FormattedID,并且没有 KeyNotFoundException:

DynamicJsonObject d = restApi.GetByReference(createResult.Reference, "FormattedID");
Console.WriteLine(d["FormattedID"]);

这是代码。我对示例进行了一些更改。删除需要工作区引用参数。由于最终缺陷被删除,您可能会在回收站中找到它。

namespace CreateDefectFromExample
{
    class Program
    {
        static void Main(string[] args)
        {

            //Initialize the REST API
            RallyRestApi restApi = new RallyRestApi("user@co.com", "TopSecret1984", "https://rally1.rallydev.com", "1.40");
            String workspaceRef = "/workspace/111111111";     //use your workspace OID
            //Create an item
            DynamicJsonObject myDefect = new DynamicJsonObject();
            myDefect["Name"] = "worst defect ever";
            CreateResult createResult = restApi.Create(workspaceRef, "defect", myDefect);
            //Console.WriteLine(myDefect["FormattedID"]); //this line causes System.Collections.Generic.KeyNotFoundException
            DynamicJsonObject d = restApi.GetByReference(createResult.Reference, "FormattedID");
            Console.WriteLine(d["FormattedID"]);


            //Update the item DynamicJsonObject toUpdate = new DynamicJsonObject();
            myDefect["Description"] = "This is my defect.";
            OperationResult updateResult = restApi.Update(createResult.Reference, myDefect);

            //Get the item
            DynamicJsonObject item = restApi.GetByReference(createResult.Reference, "Name");
            string name = item["Name"];

            //Query for items
            Request request = new Request("defect");
            request.Fetch = new List<string>()
    {
    "Name",
    "Description",
    "FormattedID"
     };
            request.Query = new Query("Name", Query.Operator.Equals, "My Defect");
            QueryResult queryResult = restApi.Query(request);
            foreach (var result in queryResult.Results)
            {
                //Process item
                string formattedID = result["FormattedID"];

            }
            //Delete the item
            OperationResult deleteResult = restApi.Delete(workspaceRef, createResult.Reference);
        }

    }
}
于 2013-06-15T20:32:34.130 回答