使用GraphServiceClient的字典是线程安全的吗?

问题描述 投票:0回答:1

我必须在我的应用程序中维护graphClients的数量。有更好的方法存储在字典中吗?线程安全吗?最好的选择是什么?

IDictionary<string, IGraphServiceClient> dict = new Dictionary<string, IGraphServiceClient>();  

    private IGraphServiceClient GetGraphServiceClient()
    {
        IDictionary<string, IGraphServiceClient> dict = new Dictionary<string, IGraphServiceClient>();
        var tenantIds = new List<string> { "Tenant1", "Tenant4", "Tenant1", "Tenant1" };
        foreach (var tenant in tenantIds)
        {
            var graphServiceClient = GetGraphServiceClient(tenant);
            var users = await graphServiceClient.Users.GetAsync();
        } 
    }

    private IGraphServiceClient GetGraphServiceClient(string i)
            {
                if (!dict.ContainsKey(i))
                {
                    dict.Add(i, new GraphServiceClient(new MyAuthenticationProvider(i)));
                }
                return dict[i];
            }
microsoft-graph microsoft-graph-sdks
1个回答
0
投票

字典不是线程安全的。而且,即使字典是线程安全的,复合语句也可以像

© www.soinside.com 2019 - 2024. All rights reserved.