当前位置: 首页 > 知识库问答 >
问题:

使用RESTAPI向Azure服务总线队列或主题发送消息

夹谷岳
2023-03-14

我正在构建Windows Phone应用程序,无法使用Microsoft。服务总线。信息。QueueClient类。

然后,我尝试使用Azure Service Bus REST API进行发送,但这需要我构建一个SAS令牌。但要构建SAS令牌,我需要使用Windows。安全密码学。果心MacAlgorithmNames。HmacSha256。此类显示在前面的类型中,但在编译时它不存在。

如何使用Send REST API将消息发布到服务总线队列或主题?

共有1个答案

马银龙
2023-03-14

我将包含使用RESTAPI向服务总线发送消息的完整代码,包括创建SAS令牌和HmacSha256哈希。

您需要使用您唯一的服务总线命名空间和密钥更新示例,并且您可能希望将其存储在比方法更好的地方...

中介绍了如何创建SAS令牌http://msdn.microsoft.com/library/azure/dn170477.aspx和https://code.msdn.microsoft.com/windowsazure/Service-Bus-HTTP-Token-38f2cfc5.

我从WindowsPhone8.1上的HmacSha256获取HmacSha256的实现?

private static void SendSBMessage(string message)
{
    try
    {
        string baseUri = "https://<your-namespace>.servicebus.windows.net";
        using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
        {
            client.BaseAddress = new Uri(baseUri);
            client.DefaultRequestHeaders.Accept.Clear();

            string token = SASTokenHelper();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedAccessSignature", token);

            string json = JsonConvert.SerializeObject(message);
            HttpContent content = new StringContent(json, Encoding.UTF8);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            string path = "/<queue-or-topic-name>/messages"; 

            var response = client.PostAsync(path, content).Result;
            if (response.IsSuccessStatusCode)
            {
                // Do something
            }
            else
            {
                // Do something else
            }
        }
    }
    catch(Exception ex)
    {
        // Handle issue
    }
}

private static string SASTokenHelper()
{
    string keyName = "RootManageSharedAccessKey";
    string key = "<your-secret-key>";
    string uri = "<your-namespace>.servicebus.windows.net";

    int expiry = (int)DateTime.UtcNow.AddMinutes(20).Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
    string stringToSign = WebUtility.UrlEncode(uri) + "\n" + expiry.ToString();
    string signature = HmacSha256(key, stringToSign);
    string token = String.Format("sr={0}&sig={1}&se={2}&skn={3}", WebUtility.UrlEncode(uri), WebUtility.UrlEncode(signature), expiry, keyName);

    return token;
}

// Because Windows.Security.Cryptography.Core.MacAlgorithmNames.HmacSha256 doesn't
// exist in WP8.1 context we need to do another implementation
public static string HmacSha256(string key, string value)
{
    var keyStrm = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
    var valueStrm = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);

    var objMacProv = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
    var hash = objMacProv.CreateHash(keyStrm);
    hash.Append(valueStrm);

    return CryptographicBuffer.EncodeToBase64String(hash.GetValueAndReset());
}
 类似资料:
  • 使用Azure Service Bus-Topics,我想实现一个解决方案,其中一旦生产者将消息发送到Topic,消息就会被发送/通知给最终消费者(比如队列)。 我理解主题作为pub/sub模型工作,其中订阅者需要从订阅中读取消息。但是我正在寻找一种类似队列的工作方法(当接收到任何消息时,它会触发web作业/服务)。 首先,我想知道服务巴士的主题是否是正确的选择?接下来,如果可能的话,实现一个变通

  • 我的应用程序中有以下代码片段,用于向Azure service bus主题队列发送消息。在发送消息的过程中,我会随机收到通用ServiceBusException。 这是我的例外, 服务无法处理请求;请重试操作。有关异常类型和正确异常处理的更多信息,请参阅http://go.microsoft.com/fwlink/?LinkId=761101TrackingId: 24e3ca8e-a74f-4

  • 问题是一旦解决了异常的来源,如何处理错误。一旦消息出错并最终出现在_error队列中,我希望在消息和/或服务修复后将消息移回处理。我无法将消息从_error队列移动到主题,因为该主题上的每个服务都将再次获得该消息。 我试图使用ReceiveEndpoint方法创建第二个队列,该方法名为_errorRecovery,但这样做会导致队列订阅主题,这意味着_errorRecovery队列获取发布到该主题

  • 我有一个windows服务,它侦听Azure服务总线队列消息,以便从我的WebApi应用程序分发处理。此外,我还需要处理重复性任务(每晚/每周),我认为最好使用相同的系统来处理这些任务。 例如,假设我有一个“CleanupDb”队列,每天午夜删除过时的DB节点: 理论上这应该行得通,但我觉得我错过了一个更明显的处理方法。有没有更好的办法?

  • 我已经创建了一个简单的窗口服务来使用来自Azure服务总线队列的消息。我使用TopShelch创建windows服务。下面的代码从这里剪切如下示例:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues 高频。Run(); ServiceBusHe

  • 我们目前正在利用Azure服务总线来处理来自应用程序的各种消息。 我想知道实时处理这些消息的最佳方式是什么? 有没有一种方法可以在消息放入队列时自动执行脚本? 我只是在想,一定有比让一个单独的应用程序每分钟/30秒检查一次队列更好的方法。 谢谢各位