编辑:
private void button1_Click(object sender, EventArgs e) {
string summary = "TestSummary";
string description = "TestDescription";
string type = "Task";
string projectKey = "TST";
string priority = "p - 3";
var issueCreated = createIssueWrapper(summary, description, type, priority, projectKey).Result;
}
public async Task <string> createIssueWrapper(string summary, string description, string type, string priority, string projectKey) {
string returnVal = "";
returnVal = await createIssue(summary, description, type, priority, projectKey);
return returnVal;
}
public async Task <string> createIssue(string summary, string description, string type, string priority, string projectKey) {
string returnVal = "";
try {
var issue = jira.CreateIssue(projectKey);
issue.Type = type;
issue.Priority = priority;
issue.Summary = summary;
issue.Description = description;
var jiraIssue = await issue.SaveChangesAsync();
if (jiraIssue != null) {
returnVal = jiraIssue.Key.ToString();
}
}
catch (Exception ex) {
returnVal = "There was a problem creating the issue. Please try again.";
}
return returnVal;
}
我一直试图弄清楚为什么Atlassian.NET Jira异步方法没有返回像常规(非异步)方法那样的异常。
作为示例,我调用一个异步方法createIssue
来创建一个新的Jira问题,如下所示:
string summary = "TestIssue";
string description = "TestDescription";
string type = "Task";
string projectKey = "TST";
string priority = "p - 3";
Task<string> created = createIssue(summary, description, type, priority, projectKey);
这是异步方法:
public async Task<string> createIssue(string summary, string description, string type, string priority, string projectKey)
{
string key = "";
try
{
var issue = jira.CreateIssue(projectKey);
issue.Type = type;
issue.Priority = priority;
issue.Summary = summary;
issue.Description = description;
var jiraIssue = await issue.SaveChangesAsync();
if (jiraIssue != null)
{
key = jiraIssue.Key.ToString();
}
}
catch (Exception ex)
{
}
return key;
}
我在await issue.SaveChangesAsync()
行上添加了一个断点,并越过了它。没有引发异常,因此代码继续等待调用完成。什么都没有告诉我有问题。
所以我将createIssue
方法转换为非异步方法:
var issue = jira.CreateIssue(projectKey);
issue.Type = type;
issue.Priority = priority;
issue.Summary = summary;
issue.Description = description;
issue.SaveChanges();
在这里,我得到一个异常,告诉我实际的问题:
找不到类型为'Atlassian.Jira.IssuePriority'的{“具有id的实体”和名称为'p-3'的实体。可用:[10000:p-3,10001:N / A,4:p-4 ,3:p-2,2:p-1,1:p-0]“}
是否可以在Async方法中捕获这些类型的异常?我需要创建处理程序还是做其他事情?
答案 0 :(得分:2)
您通过用.Result
阻止了UI线程而意外地创建了死锁。
请参阅MSDN article以进行讨论。
将按钮处理程序更改为异步并在其中使用await。另外,删除createIssueWrapper
-不需要它。