gpt4 book ai didi

xamarin.forms - 来自 Xamarin Forms PCL 的 Azure ServiceBus

转载 作者:行者123 更新时间:2023-12-04 05:35:10 28 4
gpt4 key购买 nike

如何通过 Azure 服务总线从 Xamarin Forms PCL 完成消息传递...是否有 SDK、库或插件?如果有一种方法可以手动滚动代理消息,我想它可以通过 HttpClient 和 REST API 来完成......

最佳答案

我终于有了一种从 Xamarin PCL 向 Azure 服务总线队列发布消息的工作方法!这样做的方法是通过 HttpClient。

使解决方案难以捉摸的问题:

  • Microsoft.ServiceBus.Messaging 的 nuget 包将很容易安装,但没有接触到可移植类。
  • WebClient 示例比比皆是,但在
    PCL!
  • PCL 没有用于共享访问签名的 HMACSHA256 的 native 加密
    token 。从 nuget 获取 PCLCrypto 包。
  • 一些文档说 Content-Type 的请求 header :application/atom+xml;type=entry;charset=utf-8 和 BrokerProerties 是必需的。不是这样!
  • 发布到的路径是:BaseServiceBusAddress + queue + "/messages"
    public const string ServiceBusNamespace = [YOUR SERVICEBUS NAMESPACE];

    public const string BaseServiceBusAddress = "https://" + ServiceBusNamespace + ".servicebus.windows.net/";

    /// <summary>
    /// The get shared access signature token.
    /// </summary>
    /// <param name="sasKeyName">
    /// The shared access signature key name.
    /// </param>
    /// <param name="sasKeyValue">
    /// The shared access signature key value.
    /// </param>
    /// <returns>
    /// The <see cref="string"/>.
    /// </returns>
    public static string GetSasToken(string sasKeyName, string sasKeyValue)
    {
    var expiry = GetExpiry();
    var stringToSign = WebUtility.UrlEncode(BaseServiceBusAddress ) + "\n" + expiry;

    var algorithm = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha256);
    var hasher = algorithm.CreateHash(Encoding.UTF8.GetBytes(sasKeyValue));
    hasher.Append(Encoding.UTF8.GetBytes(stringToSign));
    var mac = hasher.GetValueAndReset();
    var signature = Convert.ToBase64String(mac);

    var sasToken = string.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", WebUtility.UrlEncode(baseAddress), WebUtility.UrlEncode(signature), expiry, sasKeyName);
    return sasToken;
    }

    /// <summary>
    /// Posts an order data transfer object to queue.
    /// </summary>
    /// <param name="orderDto">
    /// The order data transfer object.
    /// </param>
    /// <param name="serviceBusNamespace">
    /// The service bus namespace.
    /// </param>
    /// <param name="sasKeyName">
    /// The shared access signature key name.
    /// </param>
    /// <param name="sasKey">
    /// The shared access signature key.
    /// </param>
    /// <param name="queue">
    /// The queue.
    /// </param>
    /// <returns>
    /// The <see cref="Task"/>.
    /// </returns>
    public static async Task<HttpResponseMessage> PostOrderDtoToQueue(OrderDto orderDto, string serviceBusNamespace, string sasKeyName, string sasKey, string queue)
    {
    using (var client = new HttpClient())
    {
    client.BaseAddress = new Uri(BaseServiceBusAddress);
    client.DefaultRequestHeaders.Accept.Clear();

    var token = GetSasToken(sasKeyName, sasKey);
    client.DefaultRequestHeaders.Add("Authorization", token);

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

    var path = BaseServiceBusAddress + queue + "/messages";

    return await client.PostAsync(path, content);
    }
    }

    /// <summary>
    /// Gets the expiry for a shared access signature token
    /// </summary>
    /// <returns>
    /// The <see cref="string" /> expiry.
    /// </returns>
    private static string GetExpiry()
    {
    var sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
    return Convert.ToString((int)sinceEpoch.TotalSeconds + 3600);
    }

    }
  • 关于xamarin.forms - 来自 Xamarin Forms PCL 的 Azure ServiceBus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39753431/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com