【问题标题】:TFS WorkItem Attachments can save but not loadTFS WorkItem Attachments 可以保存但不能加载
【发布时间】:2017-11-08 16:41:19
【问题描述】:

我可以像这样将附件保存到 WorkItem:

TfsTeamProjectCollection tfsTeamProjects = new TfsTeamProjectCollection(new Uri(tfsServerUrl));
WorkItemStore tfsWorkItemStore = tfsTeamProjects.GetService<WorkItemStore>();
WorkItem tfsWorkItem = tfsWorkItemStore.GetWorkItem(tfsWorkItemId);

FileInfo fi = new FileInfo(@"D:\\Docs\testfile.txt");
Attachment tfsAttachment = new Attachment(fi.FullName);
tfsWorkItem.Attachments.Add(tfsAttachment);
tfsWorkItem.Save();

但是当我尝试获取这样的 WorkItem 的附件列表时:

TfsTeamProjectCollection tfsTeamProjects = new TfsTeamProjectCollection(new Uri(tfsServerUrl));
WorkItemStore tfsWorkItemStore = tfsTeamProjects.GetService<WorkItemStore>();
WorkItem tfsWorkItem = tfsWorkItemStore.GetWorkItem(tfsWorkItemId);
foreach(Attachment tfsAttachment : tfsWorkItem.Attachments)
{
    // Do things here
}

tfsWorkItem.Attachments 始终为空,即使我可以在 TFS Web UI 的“对象”选项卡中看到四个附件。我得到的 WorkItem 对象是正确的。

【问题讨论】:

  • foreach 语句中不应该是in 而不是:

标签: c# tfs tfs-workitem


【解决方案1】:

我不记得所有细节,但the code I wrote a while ago 使用了一个封装类来启用 LINQ。

``` 类 AttachmentEnumerable : IEnumerable { 私有 AttachmentCollection 底层集合;

public AttachmentEnumerable(AttachmentCollection coll)
{ underlyingCollection = coll; }

public IEnumerator<Attachment> GetEnumerator()
{
    foreach (Attachment item in underlyingCollection)
    {
        yield return item;
    }
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
    return this.GetEnumerator();
}

} ```

此外,Attachment 对象仅包含一个您必须稍后下载的 URL。

【讨论】:

    【解决方案2】:

    我已经尝试过完全相同的场景,它确实对我有用。我可以附加该文件,然后使用您提到的 AttachmentCollection 获取它。您可以尝试做的是在相同的上下文中执行它,如下所示,并验证 AttachementCollection 在这种情况下是否仍然为空:

    TfsTeamProjectCollection tfsTeamProjects = new TfsTeamProjectCollection(new Uri(tfsServerUrl));
    WorkItemStore tfsWorkItemStore = tfsTeamProjects.GetService<WorkItemStore>();
    WorkItem tfsWorkItem = tfsWorkItemStore.GetWorkItem(tfsWorkItemId);
    FileInfo fi = new FileInfo(@"D:\\New Text Document.txt");
    Attachment tfsAttachment = new Attachment(fi.FullName);
    tfsWorkItem.Attachments.Add(tfsAttachment);
    tfsWorkItem.Save();
    foreach (Attachment tfsAttachment1 in tfsWorkItem.Attachments)
                    {
                        // Do things here
                    }
    

    如果此处的集合不为空,则很可能在您尝试获取附件的第二次调用中存在另一个问题。

    【讨论】:

    • 与您提供的代码相同。首先我添加了附件,我已经能够从另一个项目中获取它。我尝试的第三件事是组合方法,添加一个附件并在从 AttachmentCollection 获取它之后,在这两种情况下我都能够正确获取附件,所以我为什么向你建议组合方法,以验证如果这种情况下的集合是否为空,并根据结果进一步查看问题所在。
    • 再次阅读我的问题,我的两个示例是分开的。我不想在迭代之前添加到集合中。
    • 我读了几遍才回答。您是说“tfsWorkItem.Attachments 始终为空”,而我说它不是,无论我是通过您提供给该特定 WorkItemId 的代码添加文件,还是我选择一个已包含附件的随机 WorkItemID .因此,基于此,我相信您在调用中遇到了一些问题,要么是尝试遍历集合时出现“:”,要么是其中使用了其中一个变量。
    • Colin Steel,我相信您应该重新阅读我的答案,并删除您给我的反对票,因为我已经为您提供了信息,该做什么以及如何验证您的案例有什么问题,以及如何跟踪所采取的方法是否有问题!
    猜你喜欢
    • 2012-04-05
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2018-12-10
    • 1970-01-01
    • 2016-02-13
    相关资源
    最近更新 更多