@@ -44,7 +44,7 @@ This section assumes you have the following entities:
44
44
An input file contains a list of registrations serialized in XML, one per row. Using the Azure SDK, the following code example shows how to serialize the registrations and upload them to blob container:
45
45
46
46
``` csharp
47
- private static void SerializeToBlob (BlobContainerClient container , RegistrationDescription [] descriptions )
47
+ private static async Task SerializeToBlobAsync (BlobContainerClient container , RegistrationDescription [] descriptions )
48
48
{
49
49
StringBuilder builder = new StringBuilder ();
50
50
foreach (var registrationDescription in descriptions )
@@ -55,7 +55,7 @@ private static void SerializeToBlob(BlobContainerClient container, RegistrationD
55
55
var inputBlob = container .GetBlobClient (INPUT_FILE_NAME );
56
56
using (MemoryStream stream = new MemoryStream (Encoding .UTF8 .GetBytes (builder .ToString ())))
57
57
{
58
- inputBlob .UploadAsync (stream );
58
+ await inputBlob .UploadAsync (stream );
59
59
}
60
60
}
61
61
```
@@ -74,16 +74,12 @@ static Uri GetOutputDirectoryUrl(BlobContainerClient container)
74
74
BlobSasBuilder builder = new BlobSasBuilder (BlobSasPermissions .All , DateTime .UtcNow .AddDays (1 ));
75
75
return container .GenerateSasUri (builder );
76
76
}
77
-
78
-
79
-
80
77
81
78
static Uri GetInputFileUrl (BlobContainerClient container , string filePath )
82
79
{
83
80
Console .WriteLine (container .CanGenerateSasUri );
84
81
BlobSasBuilder builder = new BlobSasBuilder (BlobSasPermissions .Read , DateTime .UtcNow .AddDays (1 ));
85
82
return container .GenerateSasUri (builder );
86
-
87
83
}
88
84
```
89
85
@@ -93,23 +89,19 @@ With the two input and output URLs, you can now start the batch job.
93
89
94
90
``` csharp
95
91
NotificationHubClient client = NotificationHubClient .CreateClientFromConnectionString (CONNECTION_STRING , HUB_NAME );
96
- var createTask = client .SubmitNotificationHubJobAsync (
97
- new NotificationHubJob {
98
- JobType = NotificationHubJobType .ImportCreateRegistrations ,
99
- OutputContainerUri = outputContainerSasUri ,
100
- ImportFileUri = inputFileSasUri
101
- }
102
- );
103
- createTask .Wait ();
92
+ var job = await client .SubmitNotificationHubJobAsync (
93
+ new NotificationHubJob {
94
+ JobType = NotificationHubJobType .ImportCreateRegistrations ,
95
+ OutputContainerUri = outputContainerSasUri ,
96
+ ImportFileUri = inputFileSasUri
97
+ }
98
+ );
104
99
105
- var job = createTask .Result ;
106
100
long i = 10 ;
107
101
while (i > 0 && job .Status != NotificationHubJobStatus .Completed )
108
102
{
109
- var getJobTask = client .GetNotificationHubJobAsync (job .JobId );
110
- getJobTask .Wait ();
111
- job = getJobTask .Result ;
112
- Thread .Sleep (1000 );
103
+ job = await client .GetNotificationHubJobAsync (job .JobId );
104
+ await Task .Delay (1000 );
113
105
i -- ;
114
106
}
115
107
```
@@ -135,18 +127,9 @@ The following sample code imports registrations into a notification hub.
135
127
136
128
``` csharp
137
129
using Microsoft .Azure .NotificationHubs ;
138
- using Microsoft .WindowsAzure .Storage ;
139
- using Microsoft .WindowsAzure .Storage .Blob ;
140
- using System ;
141
- using System .Collections .Generic ;
142
- using System .Globalization ;
143
- using System .IO ;
144
- using System .Linq ;
145
- using System .Runtime .Serialization ;
130
+ using Azure .Storage .Blobs ;
131
+ using Azure .Storage .Sas ;
146
132
using System .Text ;
147
- using System .Threading ;
148
- using System .Threading .Tasks ;
149
- using System .Xml ;
150
133
151
134
namespace ConsoleApplication1
152
135
{
@@ -158,7 +141,7 @@ namespace ConsoleApplication1
158
141
private static string STORAGE_ACCOUNT_CONNECTIONSTRING = " connectionstring" ;
159
142
private static string CONTAINER_NAME = " containername" ;
160
143
161
- static void Main (string [] args )
144
+ static async Task Main (string [] args )
162
145
{
163
146
var descriptions = new []
164
147
{
@@ -168,45 +151,41 @@ namespace ConsoleApplication1
168
151
new MpnsRegistrationDescription (@" http://dm2.notify.live.net/throttledthirdparty/01.00/12G9Ed13dLb5RbCii5fWzpFpAgAAAAADAQAAAAQUZm52OkJCMjg1QTg1QkZDMdUxREQFBlVTTkMwMQ" ),
169
152
};
170
153
171
- // Get a reference to a container named "sample-container" and then create it
154
+ // Get a reference to a container named "sample-container" and then create it
172
155
BlobContainerClient container = new BlobContainerClient (STORAGE_ACCOUNT_CONNECTIONSTRING , CONTAINER_NAME );
173
156
174
- container .CreateIfNotExistsAsync ();
157
+ await container .CreateIfNotExistsAsync ();
175
158
176
- SerializeToBlob (container , descriptions );
159
+ await SerializeToBlobAsync (container , descriptions );
177
160
178
161
// TODO then create Sas
179
162
var outputContainerSasUri = GetOutputDirectoryUrl (container );
180
163
181
- BlobContainerClient inputfilecontainer = new BlobContainerClient (STORAGE_ACCOUNT_CONNECTIONSTRING , STORAGE_ACCOUNT_CONNECTIONSTRING + " /" + INPUT_FILE_NAME );
164
+ BlobContainerClient inputcontainer = new BlobContainerClient (STORAGE_ACCOUNT_CONNECTIONSTRING , STORAGE_ACCOUNT_CONNECTIONSTRING + " /" + INPUT_FILE_NAME );
182
165
183
166
var inputFileSasUri = GetInputFileUrl (inputcontainer , INPUT_FILE_NAME );
184
167
185
168
186
169
// Import this file
187
170
NotificationHubClient client = NotificationHubClient .CreateClientFromConnectionString (CONNECTION_STRING , HUB_NAME );
188
- var createTask = client .SubmitNotificationHubJobAsync (
171
+ var job = await client .SubmitNotificationHubJobAsync (
189
172
new NotificationHubJob {
190
173
JobType = NotificationHubJobType .ImportCreateRegistrations ,
191
174
OutputContainerUri = outputContainerSasUri ,
192
175
ImportFileUri = inputFileSasUri
193
176
}
194
177
);
195
- createTask .Wait ();
196
178
197
- var job = createTask .Result ;
198
179
long i = 10 ;
199
180
while (i > 0 && job .Status != NotificationHubJobStatus .Completed )
200
181
{
201
- var getJobTask = client .GetNotificationHubJobAsync (job .JobId );
202
- getJobTask .Wait ();
203
- job = getJobTask .Result ;
204
- Thread .Sleep (1000 );
182
+ job = await client .GetNotificationHubJobAsync (job .JobId );
183
+ await Task .Delay (1000 );
205
184
i -- ;
206
185
}
207
186
}
208
187
209
- private static void SerializeToBlob (BlobContainerClient container , RegistrationDescription [] descriptions )
188
+ private static async Task SerializeToBlobAsync (BlobContainerClient container , RegistrationDescription [] descriptions )
210
189
{
211
190
StringBuilder builder = new StringBuilder ();
212
191
foreach (var registrationDescription in descriptions )
@@ -217,7 +196,7 @@ namespace ConsoleApplication1
217
196
var inputBlob = container .GetBlobClient (INPUT_FILE_NAME );
218
197
using (MemoryStream stream = new MemoryStream (Encoding .UTF8 .GetBytes (builder .ToString ())))
219
198
{
220
- inputBlob .UploadAsync (stream );
199
+ await inputBlob .UploadAsync (stream );
221
200
}
222
201
}
223
202
0 commit comments