繁体   English   中英

Atlassian.NET SDK异步方法不会引发异常

[英]Atlassian.NET SDK Async Methods don't throw Exceptions

编辑:

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'的名称为'p-3'的ID为id的实体。可用:[10000:p-3,10001:N / A,4:p-4,3: p-2,2:p-1,1:p-0]“}

是否可以在Async方法中捕获这些类型的异常? 我需要创建处理程序还是做其他事情?

通过使用.Result阻止UI线程,意外创建了死锁。

请参阅此MSDN文章进行讨论。

将按钮处理程序更改为异步并在其中使用await。 另外,删除createIssueWrapper您不需要它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM