本文介绍了Xamarin Forms PCL的Azure ServiceBus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过Xamarin Forms PCL通过Azure Service Bus完成代理消息传递...是否有SDK,库或插件?如果可以手动发送代理消息,我想可以用HttpClient& REST API ...

How is brokered messaging via Azure Service Bus done from a Xamarin Forms PCL...is there an SDK, library or plugin? If there is a way to hand roll a brokered message, I suppose it could be accomplished with an HttpClient & the REST API ...

推荐答案

我终于有了一种有效的方法,可以将消息从Xamarin PCL发布到Azure服务总线队列中!完成此操作的方法是通过HttpClient.

I finally have a working method for posting a message to an Azure Service Bus Queue from a Xamarin PCL! The way to do this is via an HttpClient.

使解决方案难以捉摸的陷阱:

Gotchas that made the solution elusive:

  • Microsoft.ServiceBus.Messaging的nuget软件包将很高兴安装,但不会暴露于可移植类.

  • The nuget package for Microsoft.ServiceBus.Messaging will happily install, but has no exposure to the portable class.

WebClient示例比比皆是,但是PCL!

WebClient examples abound, but there is no webclient available in thePCL!

要发布的路径是:BaseServiceBusAddress +队列+"/messages"

The path to post to is: 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 PCL的Azure ServiceBus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 00:50