我必须在我的应用程序中维护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];
}
字典不是线程安全的。而且,即使字典是线程安全的,复合语句也可以像