我正在使用 Azure Mobile Services向 Windows 通用应用程序和 Azure API Management 提供数据作为 API 使用和分析目的的代理。这很好用。
现在我被要求为应用程序提供离线功能,所以我开始使用 Azure Mobile Services Synchronization Context为了使用 SQLite 作为本地存储来实现这一点。
Azure API 管理要求我将订阅 key 作为查询字符串的一部分发送。我一直在使用 IMobileServiceTable.InsertAsync 提供的“参数”字典来做这件事。方法,这也很好用。
现在离线实现要求我改用 IMobileServiceSyncTable.InsertAsync 方法,它不提供“参数”字典的重载。 MobileServiceSyncContextExtensions.PushAsync 方法似乎也没有提供将自定义参数添加到查询字符串的方法。
有人知道在使用移动服务同步上下文时包含自定义参数以发送 Azure API 管理服务的订阅 key 的方法吗?
请您参考如下方法:
我已经找到方法了。
我实现了以下 HTTP 消息处理程序:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
class AzureApiManagementHandler : DelegatingHandler
{
string _subscriptionKey;
public AzureApiManagementHandler(string subscriptionKey)
{
_subscriptionKey = subscriptionKey;
}
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
var baseUri = new UriBuilder(request.RequestUri);
string queryToAppend = string.Format("subscription-key={0}", _subscriptionKey);
if (baseUri.Query != null && baseUri.Query.Length > 1)
baseUri.Query = baseUri.Query.Substring(1) + "&" + queryToAppend;
else
baseUri.Query = queryToAppend;
request.RequestUri = baseUri.Uri;
return base.SendAsync(request, cancellationToken);
}
}
然后我在构造函数中将它传递给移动服务客户端:
public static MobileServiceClient MobileService = new MobileServiceClient(
"https://yoursubdomainhere.azure-api.net",
"yourapikeyhere",
new AzureApiManagementHandler("yoursubscriptionkeyhere")
);
我希望这对面临相同问题的任何人都有用。
