14
14
using Azure . DigitalTwins . Core . Queries ;
15
15
using Azure . DigitalTwins . Core . Serialization ;
16
16
using static Azure . DigitalTwins . Core . Samples . SampleLogger ;
17
- using static Azure . DigitalTwins . Core . Samples . UniqueIdHelper ;
18
17
19
18
namespace Azure . DigitalTwins . Core . Samples
20
19
{
@@ -50,15 +49,15 @@ internal class DigitalTwinsLifecycleSamples
50
49
private static readonly string s_twinsPath = Path . Combine ( s_dtdlDirectoryPath , "DigitalTwins" ) ;
51
50
private static readonly string s_relationshipsPath = Path . Combine ( s_dtdlDirectoryPath , "Relationships" ) ;
52
51
53
- private readonly string _eventhubEndpointName ;
54
52
private readonly string _eventRouteId = $ "sampleEventRouteId-{ Guid . NewGuid ( ) } ";
55
53
56
- private DigitalTwinsClient DigitalTwinsClient { get ; }
54
+ private readonly string eventhubEndpointName ;
55
+ private readonly DigitalTwinsClient client ;
57
56
58
57
public DigitalTwinsLifecycleSamples ( DigitalTwinsClient dtClient , string eventhubEndpointName )
59
58
{
60
- _eventhubEndpointName = eventhubEndpointName ;
61
- DigitalTwinsClient = dtClient ;
59
+ this . eventhubEndpointName = eventhubEndpointName ;
60
+ client = dtClient ;
62
61
}
63
62
64
63
/// <summary>
@@ -69,34 +68,34 @@ public DigitalTwinsLifecycleSamples(DigitalTwinsClient dtClient, string eventhub
69
68
public async Task RunSamplesAsync ( )
70
69
{
71
70
// Ensure existing twins with the same name are deleted first
72
- await DeleteTwinsAsync ( ) . ConfigureAwait ( false ) ;
71
+ await DeleteTwinsAsync ( ) ;
73
72
74
73
// Delete existing models
75
- await DeleteAllModelsAsync ( ) . ConfigureAwait ( false ) ;
74
+ await DeleteAllModelsAsync ( ) ;
76
75
77
76
// Create all the models
78
- await AddAllModelsAsync ( ) . ConfigureAwait ( false ) ;
77
+ await AddAllModelsAsync ( ) ;
79
78
80
79
// Get all models
81
- await GetAllModelsAsync ( ) . ConfigureAwait ( false ) ;
80
+ await GetAllModelsAsync ( ) ;
82
81
83
82
// Create twin counterparts for all the models
84
- await CreateAllTwinsAsync ( ) . ConfigureAwait ( false ) ;
83
+ await CreateAllTwinsAsync ( ) ;
85
84
86
85
// Get all twins
87
- await QueryTwinsAsync ( ) . ConfigureAwait ( false ) ;
86
+ await QueryTwinsAsync ( ) ;
88
87
89
88
// Create all the relationships
90
- await ConnectTwinsTogetherAsync ( ) . ConfigureAwait ( false ) ;
89
+ await ConnectTwinsTogetherAsync ( ) ;
91
90
92
91
// Creating event route
93
- await CreateEventRoute ( ) . ConfigureAwait ( false ) ;
92
+ await CreateEventRoute ( ) ;
94
93
95
94
// Get all event routes
96
- await GetEventRoutes ( ) . ConfigureAwait ( false ) ;
95
+ await GetEventRoutes ( ) ;
97
96
98
97
// Deleting event route
99
- await DeleteEventRoute ( ) . ConfigureAwait ( false ) ;
98
+ await DeleteEventRoute ( ) ;
100
99
}
101
100
102
101
/// <summary>
@@ -119,7 +118,7 @@ private async Task DeleteAllModelsAsync()
119
118
120
119
foreach ( string modelId in models )
121
120
{
122
- await DigitalTwinsClient . DeleteModelAsync ( modelId ) . ConfigureAwait ( false ) ;
121
+ await client . DeleteModelAsync ( modelId ) ;
123
122
Console . WriteLine ( $ "Deleted model { modelId } ") ;
124
123
}
125
124
}
@@ -149,7 +148,7 @@ private async Task AddAllModelsAsync()
149
148
150
149
try
151
150
{
152
- Response < IReadOnlyList < ModelData > > response = await DigitalTwinsClient . CreateModelsAsync ( modelsToCreate ) . ConfigureAwait ( false ) ;
151
+ Response < IReadOnlyList < ModelData > > response = await client . CreateModelsAsync ( modelsToCreate ) ;
153
152
Console . WriteLine ( $ "Created models status: { response . GetRawResponse ( ) . Status } ") ;
154
153
}
155
154
catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . Conflict )
@@ -174,15 +173,15 @@ public async Task GetAllModelsAsync()
174
173
175
174
#region Snippet:DigitalTwinsSampleGetModels
176
175
177
- AsyncPageable < ModelData > allModels = DigitalTwinsClient . GetModelsAsync ( ) ;
176
+ AsyncPageable < ModelData > allModels = client . GetModelsAsync ( ) ;
178
177
await foreach ( ModelData model in allModels )
179
178
{
180
179
Console . WriteLine ( $ "Model Id: { model . Id } , display name: { model . DisplayName [ "en" ] } , upload time: { model . UploadTime } , is decommissioned: { model . Decommissioned } ") ;
181
180
}
182
181
183
182
#endregion Snippet:DigitalTwinsSampleGetModels
184
183
}
185
- catch ( Exception ex )
184
+ catch ( RequestFailedException ex )
186
185
{
187
186
FatalError ( $ "Failed to get all the models due to:\n { ex } ") ;
188
187
}
@@ -204,36 +203,36 @@ public async Task DeleteTwinsAsync()
204
203
205
204
#region Snippet:DigitalTwinsSampleGetRelationships
206
205
207
- AsyncPageable < string > relationships = DigitalTwinsClient . GetRelationshipsAsync ( twin . Key ) ;
206
+ AsyncPageable < string > relationships = client . GetRelationshipsAsync ( twin . Key ) ;
208
207
209
208
#endregion Snippet:DigitalTwinsSampleGetRelationships
210
209
211
210
await foreach ( var relationshipJson in relationships )
212
211
{
213
212
BasicRelationship relationship = JsonSerializer . Deserialize < BasicRelationship > ( relationshipJson ) ;
214
- await DigitalTwinsClient . DeleteRelationshipAsync ( twin . Key , relationship . Id ) . ConfigureAwait ( false ) ;
213
+ await client . DeleteRelationshipAsync ( twin . Key , relationship . Id ) ;
215
214
Console . WriteLine ( $ "Found and deleted relationship { relationship . Id } ") ;
216
215
}
217
216
218
217
// Delete any incoming relationships
219
218
220
219
#region Snippet:DigitalTwinsSampleGetIncomingRelationships
221
220
222
- AsyncPageable < IncomingRelationship > incomingRelationships = DigitalTwinsClient . GetIncomingRelationshipsAsync ( twin . Key ) ;
221
+ AsyncPageable < IncomingRelationship > incomingRelationships = client . GetIncomingRelationshipsAsync ( twin . Key ) ;
223
222
224
223
#endregion Snippet:DigitalTwinsSampleGetIncomingRelationships
225
224
226
225
await foreach ( IncomingRelationship incomingRelationship in incomingRelationships )
227
226
{
228
- await DigitalTwinsClient . DeleteRelationshipAsync ( incomingRelationship . SourceId , incomingRelationship . RelationshipId ) . ConfigureAwait ( false ) ;
227
+ await client . DeleteRelationshipAsync ( incomingRelationship . SourceId , incomingRelationship . RelationshipId ) ;
229
228
Console . WriteLine ( $ "Found and deleted incoming relationship { incomingRelationship . RelationshipId } ") ;
230
229
}
231
230
232
231
// Now the digital twin should be safe to delete
233
232
234
233
#region Snippet:DigitalTwinsSampleDeleteTwin
235
234
236
- await DigitalTwinsClient . DeleteDigitalTwinAsync ( twin . Key ) . ConfigureAwait ( false ) ;
235
+ await client . DeleteDigitalTwinAsync ( twin . Key ) ;
237
236
238
237
#endregion Snippet:DigitalTwinsSampleDeleteTwin
239
238
@@ -263,7 +262,7 @@ public async Task CreateAllTwinsAsync()
263
262
{
264
263
try
265
264
{
266
- Response < string > response = await DigitalTwinsClient . CreateDigitalTwinAsync ( twin . Key , twin . Value ) . ConfigureAwait ( false ) ;
265
+ Response < string > response = await client . CreateDigitalTwinAsync ( twin . Key , twin . Value ) ;
267
266
268
267
Console . WriteLine ( $ "Created digital twin { twin . Key } . Create response status: { response . GetRawResponse ( ) . Status } ") ;
269
268
Console . WriteLine ( $ "Body: { response ? . Value } ") ;
@@ -287,7 +286,7 @@ public async Task QueryTwinsAsync()
287
286
288
287
// This code snippet demonstrates the simplest way to iterate over the digital twin results, where paging
289
288
// happens under the covers.
290
- AsyncPageable < string > asyncPageableResponse = DigitalTwinsClient . QueryAsync ( "SELECT * FROM digitaltwins" ) ;
289
+ AsyncPageable < string > asyncPageableResponse = client . QueryAsync ( "SELECT * FROM digitaltwins" ) ;
291
290
292
291
// Iterate over the twin instances in the pageable response.
293
292
// The "await" keyword here is required because new pages will be fetched when necessary,
@@ -308,7 +307,7 @@ public async Task QueryTwinsAsync()
308
307
// the query API. It iterates over the response pages first to access to the query-charge header,
309
308
// and then the digital twin results within each page.
310
309
311
- AsyncPageable < string > asyncPageableResponseWithCharge = DigitalTwinsClient . QueryAsync ( "SELECT * FROM digitaltwins" ) ;
310
+ AsyncPageable < string > asyncPageableResponseWithCharge = client . QueryAsync ( "SELECT * FROM digitaltwins" ) ;
312
311
int pageNum = 0 ;
313
312
314
313
// The "await" keyword here is required as a call is made when fetching a new page.
@@ -363,12 +362,10 @@ public async Task ConnectTwinsTogetherAsync()
363
362
364
363
string serializedRelationship = JsonSerializer . Serialize ( relationship ) ;
365
364
366
- await DigitalTwinsClient
367
- . CreateRelationshipAsync (
368
- relationship . SourceId ,
369
- relationship . Id ,
370
- serializedRelationship )
371
- . ConfigureAwait ( false ) ;
365
+ await client . CreateRelationshipAsync (
366
+ relationship . SourceId ,
367
+ relationship . Id ,
368
+ serializedRelationship ) ;
372
369
373
370
#endregion Snippet:DigitalTwinsSampleCreateRelationship
374
371
@@ -392,7 +389,7 @@ public async Task GetEventRoutes()
392
389
{
393
390
#region Snippet:DigitalTwinsSampleGetEventRoutes
394
391
395
- AsyncPageable < EventRoute > response = DigitalTwinsClient . GetEventRoutesAsync ( ) ;
392
+ AsyncPageable < EventRoute > response = client . GetEventRoutesAsync ( ) ;
396
393
await foreach ( EventRoute er in response )
397
394
{
398
395
Console . WriteLine ( $ "Event route: { er . Id } , endpoint name: { er . EndpointName } ") ;
@@ -417,12 +414,12 @@ public async Task CreateEventRoute()
417
414
#region Snippet:DigitalTwinsSampleCreateEventRoute
418
415
419
416
string eventFilter = "$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'" ;
420
- var eventRoute = new EventRoute ( _eventhubEndpointName )
417
+ var eventRoute = new EventRoute ( eventhubEndpointName )
421
418
{
422
419
Filter = eventFilter
423
420
} ;
424
421
425
- Response createEventRouteResponse = await DigitalTwinsClient . CreateEventRouteAsync ( _eventRouteId , eventRoute ) . ConfigureAwait ( false ) ;
422
+ Response createEventRouteResponse = await client . CreateEventRouteAsync ( _eventRouteId , eventRoute ) ;
426
423
427
424
#endregion Snippet:DigitalTwinsSampleCreateEventRoute
428
425
@@ -444,7 +441,7 @@ public async Task DeleteEventRoute()
444
441
{
445
442
#region Snippet:DigitalTwinsSampleDeleteEventRoute
446
443
447
- Response response = await DigitalTwinsClient . DeleteEventRouteAsync ( _eventRouteId ) . ConfigureAwait ( false ) ;
444
+ Response response = await client . DeleteEventRouteAsync ( _eventRouteId ) ;
448
445
449
446
#endregion Snippet:DigitalTwinsSampleDeleteEventRoute
450
447
0 commit comments