c# - 使用 Graph 创建团队后是否需要等待

标签 c# azure microsoft-graph-api microsoft-teams microsoft-graph-teams

我正在使用我们的网络应用程序中的 MS Graph API 在客户的系统中创建 MS Teams 团队并设置一些文件夹。但如果我在创建团队后不强制执行硬编码等待,我会随机出现错误。我按所示顺序调用以下端点:

//Create new Team and get basic info
POST teams
GET teams/{team-id}/primaryChannel
GET teams/{team-id}
GET teams/{team-id}/channels/{channel-id}/filesFolder

//Sometimes unknown users must be invited to join org as guest
POST invitations

//Everyone but the owner is added as a guest
POST teams/{team-id}/members

//This is done in a batch, because there is one folder per team guest + one for owner
POST groups/{team-id}/drive/items/{channel-folder-id}/children

//Team members' folders are permitted to them only. So all permissions are deleted and a single user added back
GET groups/{folder-id}/drive/items/{folder-id}/permissions
DELETE groups/{team-id}/drive/items/{folder-id}/permissions/{permission-id}
POST groups/{folder-id}/drive/items/{item-id}/invite

我会偶尔收到来自以下来源的禁止和/或错误请求响应:

POST teams/{team-id}/members
DELETE - groups/{team-id}/drive/items/{item-id}/permissions/{permission-id}

显然 403 的返回状态是错误,因为应用程序肯定有执行该操作的权限。

创建团队后施加 60 秒的等待似乎可以解决此问题。但是,我目前正在我们的 Teams 环境上进行测试,并担心拥有较大 Teams 设置的客户将需要更长的等待时间。我见过其他领域的文档说您应该等待最多 15 分钟,然后才能使用从组创建的团队(不过我不确定这是否适用于创建普通团队)。

有谁知道我通常应该为什么样的延迟做好准备,以及是否有任何端点我可以 ping 一下以查看团队是否准备好使用?

最佳答案

Azure AD、Teams 和 Exchange 都是不同的系统,需要某种同步,有时需要一些时间。

每当您要在其中一个系统中创建某些内容时,请做好准备,访问它需要一些时间。

我遇到的最尴尬的行为之一是,当您通过 Exchange Remote Powershell 创建组时,您将立即取回组对象。该对象有一个 Azure 对象 ID。但是,如果您立即转到 Graph 并向该组发出请求,您将收到 404。此外,查看 Azure Portal 也不会显示任何内容。但如果你等待一段时间(最短 30 秒,最多 20 分钟),该群体就会突然出现。

如果您通过 Graph 在 Azure 中创建用户,这同样适用。如果这样做,您将返回一个具有 azure id 的对象。如果您立即尝试将此用户添加到组或目录角色中,也可能会遇到错误,但这里的超时通常低于 2 秒,我从未见过超过 10 秒的情况。

因此,对于所有事情,我要在 Graph 中创建一些内容并立即尝试使用它,我构建了一些辅助方法,该方法会多次尝试,每次调用之间的超时时间较小:

internal static class Multiple
{
    public static Task Try<TException>(int maxRetries, TimeSpan interval, Func<Task> task)
        where TException : Exception
    {
        return Try<TException>(maxRetries, interval, task, exception => true);
    }

    public static async Task Try<TException>(int maxRetries, TimeSpan interval, Func<Task> task, Func<TException, bool> isExpectedException)
        where TException : Exception
    {
        do
        {
            try
            {
                await task().ConfigureAwait(false);
                return;
            }
            catch (Exception ex) when (ex.GetType() == typeof(TException) && isExpectedException((TException)ex))
            {
                maxRetries--;

                if (maxRetries <= 0)
                    throw;

                await Task.Delay(interval);
            }
        } while (true);
    }
}

该类的用法如下:

await Multiple.Try<ServiceException>(20, TimeSpan.FromSeconds(1), async () =>
{
    educationClass = await serviceClient.Education.Classes[groupId.ToString()].Request().GetAsync();
}, ex => ex.Error.Code == "Request_ResourceNotFound");

该助手将调用内部方法最多 20 次,超时时间为 1 秒。此外,抛出的异常必须具有给定的错误代码。如果超过重试次数或抛出不同的错误,则调用将重新抛出原始异常,并且必须在更高级别上进行处理。

只需注意,在 Graph 界面后面,一个高度分布式的系统在工作,有时需要一些时间才能使所有内容同步。

关于c# - 使用 Graph 创建团队后是否需要等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67077990/

相关文章:

c# - C# Parent 和 Child 中的 MDI 窗体

azure - Microsoft Graph API 是否可以与 Office 365 家庭订阅一起使用?

c# - 不支持协议(protocol) 'net.tcp'

c# - 需要 .NET 代码仅在调试配置时执行

azure - 配置 Azure 搜索停用词

Azure 发布管道,失败阶段后继续

azure - 容器应用程序环境中批处理作业的调度执行

microsoft-graph-api - 在 OneDrive 图形 api 上为特殊 AppFolder 创建上传 session 时出错

azure - 不存在与模板匹配的 OData 路由 - 在 Azure intune 中添加 CorporateIdentifier

c# - WPF:在输入时将焦点移至 ItemsControl 中的下一个项目