使用.NET工具包页面here中的创建缺陷示例。
尝试使用DynamicJsonObject时出现以下错误 -
toCreate [“Name”]'toCreate [“Name”]'抛出类型'System.Collections.Generic.KeyNotFoundException'的异常'dynamic {System.Collections.Generic.KeyNotFoundException}
我正在使用的代码就像示例所示:
DynamicJsonObject toCreate = new DynamicJsonObject();
toCreate["Name"] = "My Defect";
CreateResult createResult = restApi.Create(workspaceRef, "defect", toCreate);
这是我的问题还是API中存在缺陷?
答案 0 :(得分: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);
}
}
}