-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
73 lines (64 loc) · 3.06 KB
/
Copy pathProgram.cs
File metadata and controls
73 lines (64 loc) · 3.06 KB
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
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Web;
class Program
{
static async Task Main()
{
// Replace with your Service Bus details
string serviceNamespace = "Add the Service Bus Name Space Here";
string topicName = "The Topic/Queue Name goes Here";
string messageBody = "This is a message.";
// Create the HTTP client
using HttpClient client = new();
// Set the request URI
string requestUri = $"https://{serviceNamespace}.servicebus.windows.net/{topicName}/messages?timeout=60";
string sasToken = GetSasToken(requestUri, "Add Shared Access Policy Key Name", "Add Shared Access Policy Key Value", TimeSpan.FromDays(1));
// Create the message content with corrected content type
StringContent content = new(messageBody, Encoding.UTF8, "application/xml");
// Add required headers
client.DefaultRequestHeaders.Add("Authorization", sasToken);
string messageId = Guid.NewGuid().ToString();
//Setting the broker properties
Dictionary<string, string> brokerProperties = new()
{
{ "CorrelationId", messageId },
{ "SessionId", "123" }
};
// Adding the BrokerProperties header with SessionId
client.DefaultRequestHeaders.Add("BrokerProperties", JsonSerializer.Serialize(brokerProperties));
//Setting the Custom Properties
client.DefaultRequestHeaders.Add("message_type", "My test");
client.DefaultRequestHeaders.Add("message_id", messageId);
client.DefaultRequestHeaders.ExpectContinue = true;
// Service Bus Endpoint Call
HttpResponseMessage response = await client.PostAsync(requestUri, content);
// Check the response
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Message sent successfully with status code: {response.StatusCode}");
}
else
{
Console.WriteLine($"Failed to send message. Status code: {response.StatusCode}, Reason: {response.ReasonPhrase}");
}
}
public static string GetSasToken(string resourceUri, string keyName, string key, TimeSpan ttl)
{
var expiry = GetExpiry(ttl);
string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new(Encoding.UTF8.GetBytes(key));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = string.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
return sasToken;
}
private static string GetExpiry(TimeSpan ttl)
{
TimeSpan expirySinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1) + ttl;
return Convert.ToString((int)expirySinceEpoch.TotalSeconds);
}
}