-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSnippets.cs
199 lines (154 loc) · 6.84 KB
/
Snippets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.Core;
using Microsoft.Azure.Storage.Auth;
using Newtonsoft.Json;
class Snippets
{
void ConfigurationAndRegistration(string connectionString, string queueName, string storageConnectionString)
{
#region ConfigurationAndRegistration
var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString);
sender.RegisterAzureStorageAttachmentPlugin(config);
#endregion
}
[SuppressMessage("ReSharper", "UnusedVariable")]
void AttachmentSending()
{
#region AttachmentSending
var payload = new MyMessage
{
MyProperty = "The Value"
};
var serialized = JsonConvert.SerializeObject(payload);
var payloadAsBytes = Encoding.UTF8.GetBytes(serialized);
var message = new Message(payloadAsBytes);
#endregion
}
[SuppressMessage("ReSharper", "UnusedVariable")]
async Task AttachmentReceiving(string connectionString, string entityPath, AzureStorageAttachmentConfiguration config)
{
#region AttachmentReceiving
var receiver = new MessageReceiver(connectionString, entityPath, ReceiveMode.ReceiveAndDelete);
receiver.RegisterAzureStorageAttachmentPlugin(config);
var message = await receiver.ReceiveAsync().ConfigureAwait(false);
// message will contain the original payload
#endregion
}
void ConfigurationAndRegistrationSas(string connectionString, string queueName, string storageConnectionString)
{
#region ConfigurationAndRegistrationSas
var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString)
.WithBlobSasUri(
sasTokenValidationTime: TimeSpan.FromHours(4),
messagePropertyToIdentifySasUri: "mySasUriProperty");
sender.RegisterAzureStorageAttachmentPlugin(config);
#endregion
}
void Configure_blob_name_override(string connectionString, string queueName, string storageConnectionString)
{
#region Configure_blob_name_override
var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString)
.OverrideBlobName(message =>
{
var tenantId = message.UserProperties["tenantId"].ToString();
var blobName = $"{tenantId}/{message.MessageId}";
return blobName;
});
sender.RegisterAzureStorageAttachmentPlugin(config);
#endregion
}
void Configure_blob_sas_uri_override(string storageConnectionString)
{
#region Configure_blob_sas_uri_override
new AzureStorageAttachmentConfiguration(storageConnectionString)
.WithBlobSasUri(
messagePropertyToIdentifySasUri: "mySasUriProperty",
sasTokenValidationTime: TimeSpan.FromHours(12));
#endregion
}
void Configure_body_override(string connectionString, string queueName, string storageConnectionString)
{
#region Configure_body_override
var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString)
.OverrideBody(message => Array.Empty<byte>());
sender.RegisterAzureStorageAttachmentPlugin(config);
#endregion
}
[SuppressMessage("ReSharper", "UnusedVariable")]
void AttachmentSendingSas()
{
#region AttachmentSendingSas
var payload = new MyMessage
{
MyProperty = "The Value"
};
var serialized = JsonConvert.SerializeObject(payload);
var payloadAsBytes = Encoding.UTF8.GetBytes(serialized);
var message = new Message(payloadAsBytes);
#endregion
}
[SuppressMessage("ReSharper", "UnusedVariable")]
async Task AttachmentReceivingSas(MessageReceiver messageReceiver)
{
#region AttachmentReceivingSas
// Override message property used to identify SAS URI
// .RegisterAzureStorageAttachmentPluginForReceivingOnly() is using "$attachment.sas.uri" by default
messageReceiver.RegisterAzureStorageAttachmentPluginForReceivingOnly("mySasUriProperty");
var message = await messageReceiver.ReceiveAsync().ConfigureAwait(false);
#endregion
}
void Configure_criteria_for_message_max_size_identification(string storageConnectionString)
{
#region Configure_criteria_for_message_max_size_identification
// messages with body > 200KB should be converted to use attachments
new AzureStorageAttachmentConfiguration(storageConnectionString,
messageMaxSizeReachedCriteria: message => message.Body.Length > 200 * 1024);
#endregion
}
[SuppressMessage("ReSharper", "UnusedVariable")]
void Configuring_connection_string_provider(string connectionString)
{
#region Configuring_connection_string_provider
var provider = new PlainTextConnectionStringProvider(connectionString);
var config = new AzureStorageAttachmentConfiguration(provider);
#endregion
}
[SuppressMessage("ReSharper", "UnusedVariable")]
void Configuring_plugin_using_StorageCredentials(string connectionString, string blobEndpoint)
{
#region Configuring_plugin_using_StorageCredentials
var credentials = new StorageCredentials( /*Shared key OR Service SAS OR Container SAS*/);
var config = new AzureStorageAttachmentConfiguration(credentials, blobEndpoint);
#endregion
}
async Task Upload_attachment_without_registering_plugin(Message message, AzureStorageAttachmentConfiguration config)
{
#region Upload_attachment_without_registering_plugin
//To make it possible to use SAS URI when downloading, use WithBlobSasUri() when creating configuration object
await message.UploadAzureStorageAttachment(config);
#endregion
}
async Task Download_attachment_without_registering_plugin(Message message, AzureStorageAttachmentConfiguration config)
{
#region Download_attachment_without_registering_plugin
//Using SAS URI with default message property ($attachment.sas.uri)
await message.DownloadAzureStorageAttachment();
//Using SAS URI with custom message property
await message.DownloadAzureStorageAttachment("$custom-attachment.sas.uri");
//Using configuration object
await message.DownloadAzureStorageAttachment(config);
#endregion
}
class MyMessage
{
public string MyProperty { get; set; } = string.Empty;
}
}