Skip to content

Commit 43bbc7d

Browse files
committed
Add CancellationToken support and improve error handling
Introduced CancellationToken support across various methods in IAsyncEnumerableGraphDataService, GraphDataService, and related implementations to allow for operation cancellation. Enhanced error handling with ErrorMappings and ShowDialogAsync methods. Simplified and refactored IsBusyWrapper methods in MainViewModel.
1 parent 0938fbf commit 43bbc7d

File tree

11 files changed

+199
-186
lines changed

11 files changed

+199
-186
lines changed

MsGraphSamples.Services/AsyncEnumerableGraphDataService.cs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@
88

99
namespace MsGraphSamples.Services;
1010

11-
1211
public interface IAsyncEnumerableGraphDataService
1312
{
1413
string? LastUrl { get; }
1514
public long? LastCount { get; }
1615

1716
Task<User?> GetUserAsync(string[] select, string? id = null);
18-
IAsyncEnumerable<User> GetUsersInBatch(string[] select, ushort top = 999);
19-
IAsyncEnumerable<Application> GetApplications(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999);
20-
IAsyncEnumerable<ServicePrincipal> GetServicePrincipals(string[] splittedSelect, string? filter, string[]? splittedOrderBy, string? search, ushort top = 999);
21-
IAsyncEnumerable<Device> GetDevices(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999);
22-
IAsyncEnumerable<Group> GetGroups(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999);
23-
IAsyncEnumerable<User> GetUsers(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999);
24-
IAsyncEnumerable<User> GetApplicationOwnersAsUsers(string id, string[] select, ushort top = 999);
25-
IAsyncEnumerable<User> GetServicePrincipalOwnersAsUsers(string id, string[] select, ushort top = 999);
26-
IAsyncEnumerable<User> GetDeviceOwnersAsUsers(string id, string[] select, ushort top = 999);
27-
IAsyncEnumerable<User> GetGroupOwnersAsUsers(string id, string[] select, ushort top = 999);
28-
IAsyncEnumerable<Group> GetTransitiveMemberOfAsGroups(string id, string[] select, ushort top = 999);
29-
IAsyncEnumerable<User> GetTransitiveMembersAsUsers(string id, string[] select, ushort top = 999);
17+
IAsyncEnumerable<User> GetUsersInBatch(string[] select, ushort top = 999, CancellationToken cancellationToken = default);
18+
IAsyncEnumerable<Application> GetApplications(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999, CancellationToken cancellationToken = default);
19+
IAsyncEnumerable<ServicePrincipal> GetServicePrincipals(string[] splittedSelect, string? filter, string[]? splittedOrderBy, string? search, ushort top = 999, CancellationToken cancellationToken = default);
20+
IAsyncEnumerable<Device> GetDevices(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999, CancellationToken cancellationToken = default);
21+
IAsyncEnumerable<Group> GetGroups(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999, CancellationToken cancellationToken = default);
22+
IAsyncEnumerable<User> GetUsers(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999, CancellationToken cancellationToken = default);
23+
IAsyncEnumerable<User> GetApplicationOwnersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default);
24+
IAsyncEnumerable<User> GetServicePrincipalOwnersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default);
25+
IAsyncEnumerable<User> GetDeviceOwnersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default);
26+
IAsyncEnumerable<User> GetGroupOwnersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default);
27+
IAsyncEnumerable<Group> GetTransitiveMemberOfAsGroups(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default);
28+
IAsyncEnumerable<User> GetTransitiveMembersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default);
3029
}
3130

3231
public class AsyncEnumerableGraphDataService(GraphServiceClient graphClient) : IAsyncEnumerableGraphDataService
3332
{
3433
private readonly GraphServiceClient _graphClient = graphClient;
3534
private readonly RequestHeaders EventualConsistencyHeader = new() { { "ConsistencyLevel", "eventual" } };
35+
3636
public string? LastUrl { get; private set; } = null;
3737
public long? LastCount { get; private set; } = null;
3838
private void SetCount(long? count) => LastCount = count;
@@ -44,9 +44,9 @@ public class AsyncEnumerableGraphDataService(GraphServiceClient graphClient) : I
4444
: _graphClient.Users[id].GetAsync(rc => rc.QueryParameters.Select = select);
4545
}
4646

47-
public IAsyncEnumerable<User> GetUsersInBatch(string[] select, ushort top = 999)
47+
public IAsyncEnumerable<User> GetUsersInBatch(string[] select, ushort top = 999, CancellationToken cancellationToken = default)
4848
{
49-
return _graphClient.Batch<User, UserCollectionResponse>(
49+
return _graphClient.Batch<User, UserCollectionResponse>(cancellationToken,
5050
_graphClient.Users.ToGetRequestInformation(rc =>
5151
{
5252
rc.Headers = EventualConsistencyHeader;
@@ -73,7 +73,7 @@ public IAsyncEnumerable<User> GetUsersInBatch(string[] select, ushort top = 999)
7373
}));
7474
}
7575

76-
public IAsyncEnumerable<Application> GetApplications(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999)
76+
public IAsyncEnumerable<Application> GetApplications(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999, CancellationToken cancellationToken = default)
7777
{
7878
var requestInfo = _graphClient.Applications
7979
.ToGetRequestInformation(rc =>
@@ -88,10 +88,10 @@ public IAsyncEnumerable<Application> GetApplications(string[] select, string? fi
8888
});
8989

9090
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
91-
return requestInfo.ToAsyncEnumerable<Application, ApplicationCollectionResponse>(_graphClient.RequestAdapter, SetCount);
91+
return requestInfo.ToAsyncEnumerable<Application, ApplicationCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
9292
}
9393

94-
public IAsyncEnumerable<ServicePrincipal> GetServicePrincipals(string[] select, string? filter, string[]? orderBy, string? search = null, ushort top = 999)
94+
public IAsyncEnumerable<ServicePrincipal> GetServicePrincipals(string[] select, string? filter, string[]? orderBy, string? search = null, ushort top = 999, CancellationToken cancellationToken = default)
9595
{
9696
var requestInfo = _graphClient.ServicePrincipals
9797
.ToGetRequestInformation(rc =>
@@ -106,10 +106,10 @@ public IAsyncEnumerable<ServicePrincipal> GetServicePrincipals(string[] select,
106106
});
107107

108108
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
109-
return requestInfo.ToAsyncEnumerable<ServicePrincipal, ServicePrincipalCollectionResponse>(_graphClient.RequestAdapter, SetCount);
109+
return requestInfo.ToAsyncEnumerable<ServicePrincipal, ServicePrincipalCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
110110
}
111111

112-
public IAsyncEnumerable<Device> GetDevices(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999)
112+
public IAsyncEnumerable<Device> GetDevices(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999, CancellationToken cancellationToken = default)
113113
{
114114
var requestInfo = _graphClient.Devices
115115
.ToGetRequestInformation(rc =>
@@ -124,10 +124,10 @@ public IAsyncEnumerable<Device> GetDevices(string[] select, string? filter = nul
124124
});
125125

126126
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
127-
return requestInfo.ToAsyncEnumerable<Device, DeviceCollectionResponse>(_graphClient.RequestAdapter, SetCount);
127+
return requestInfo.ToAsyncEnumerable<Device, DeviceCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
128128
}
129129

130-
public IAsyncEnumerable<Group> GetGroups(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999)
130+
public IAsyncEnumerable<Group> GetGroups(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999, CancellationToken cancellationToken = default)
131131
{
132132
var requestInfo = _graphClient.Groups
133133
.ToGetRequestInformation(rc =>
@@ -142,10 +142,10 @@ public IAsyncEnumerable<Group> GetGroups(string[] select, string? filter = null,
142142
});
143143

144144
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
145-
return requestInfo.ToAsyncEnumerable<Group, GroupCollectionResponse>(_graphClient.RequestAdapter, SetCount);
145+
return requestInfo.ToAsyncEnumerable<Group, GroupCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
146146
}
147147

148-
public IAsyncEnumerable<User> GetUsers(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999)
148+
public IAsyncEnumerable<User> GetUsers(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999, CancellationToken cancellationToken = default)
149149
{
150150
var requestInfo = _graphClient.Users
151151
.ToGetRequestInformation(rc =>
@@ -160,10 +160,10 @@ public IAsyncEnumerable<User> GetUsers(string[] select, string? filter = null, s
160160
});
161161

162162
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
163-
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount);
163+
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
164164
}
165165

166-
public IAsyncEnumerable<User> GetApplicationOwnersAsUsers(string id, string[] select, ushort top = 999)
166+
public IAsyncEnumerable<User> GetApplicationOwnersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default)
167167
{
168168
var requestInfo = _graphClient.Applications[id]
169169
.Owners.GraphUser
@@ -176,10 +176,10 @@ public IAsyncEnumerable<User> GetApplicationOwnersAsUsers(string id, string[] se
176176
});
177177

178178
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
179-
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount);
179+
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
180180
}
181181

182-
public IAsyncEnumerable<User> GetServicePrincipalOwnersAsUsers(string id, string[] select, ushort top = 999)
182+
public IAsyncEnumerable<User> GetServicePrincipalOwnersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default)
183183
{
184184
var requestInfo = _graphClient.ServicePrincipals[id]
185185
.Owners.GraphUser
@@ -192,10 +192,10 @@ public IAsyncEnumerable<User> GetServicePrincipalOwnersAsUsers(string id, string
192192
});
193193

194194
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
195-
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount);
195+
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
196196
}
197197

198-
public IAsyncEnumerable<User> GetDeviceOwnersAsUsers(string id, string[] select, ushort top = 999)
198+
public IAsyncEnumerable<User> GetDeviceOwnersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default)
199199
{
200200
var requestInfo = _graphClient.Devices[id]
201201
.RegisteredOwners.GraphUser
@@ -208,10 +208,10 @@ public IAsyncEnumerable<User> GetDeviceOwnersAsUsers(string id, string[] select,
208208
});
209209

210210
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
211-
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount);
211+
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
212212
}
213213

214-
public IAsyncEnumerable<User> GetGroupOwnersAsUsers(string id, string[] select, ushort top = 999)
214+
public IAsyncEnumerable<User> GetGroupOwnersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default)
215215
{
216216
var requestInfo = _graphClient.Groups[id]
217217
.Owners.GraphUser
@@ -225,10 +225,10 @@ public IAsyncEnumerable<User> GetGroupOwnersAsUsers(string id, string[] select,
225225
});
226226

227227
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
228-
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount);
228+
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
229229
}
230230

231-
public IAsyncEnumerable<Group> GetTransitiveMemberOfAsGroups(string id, string[] select, ushort top = 999)
231+
public IAsyncEnumerable<Group> GetTransitiveMemberOfAsGroups(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default)
232232
{
233233
var requestInfo = _graphClient.Users[id]
234234
.TransitiveMemberOf.GraphGroup
@@ -241,10 +241,10 @@ public IAsyncEnumerable<Group> GetTransitiveMemberOfAsGroups(string id, string[]
241241
});
242242

243243
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
244-
return requestInfo.ToAsyncEnumerable<Group, GroupCollectionResponse>(_graphClient.RequestAdapter, SetCount);
244+
return requestInfo.ToAsyncEnumerable<Group, GroupCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
245245
}
246246

247-
public IAsyncEnumerable<User> GetTransitiveMembersAsUsers(string id, string[] select, ushort top = 999)
247+
public IAsyncEnumerable<User> GetTransitiveMembersAsUsers(string id, string[] select, ushort top = 999, CancellationToken cancellationToken = default)
248248
{
249249
var requestInfo = _graphClient.Groups[id]
250250
.TransitiveMembers.GraphUser
@@ -257,6 +257,6 @@ public IAsyncEnumerable<User> GetTransitiveMembersAsUsers(string id, string[] se
257257
});
258258

259259
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
260-
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount);
260+
return requestInfo.ToAsyncEnumerable<User, UserCollectionResponse>(_graphClient.RequestAdapter, SetCount, cancellationToken);
261261
}
262262
}

0 commit comments

Comments
 (0)