【问题标题】:Azure Mobile Services Sync Context: Adding custom parametersAzure 移动服务同步上下文:添加自定义参数
【发布时间】:2014-12-02 00:09:17
【问题描述】:

我使用 Azure Mobile Services 向 Windows 通用应用程序提供数据,并使用 Azure API Management 作为 API 使用和分析目的的代理。这很好用。

现在我被要求为应用程序提供离线功能,因此我开始使用Azure Mobile Services Synchronization Context 以使用 SQLite 作为本地存储来实现此功能。

Azure API 管理要求我将订阅密钥作为查询字符串的一部分发送。我一直在使用IMobileServiceTable.InsertAsync 方法提供的“参数”字典来执行此操作,并且效果也很好。

现在脱机实现需要我改用 IMobileServiceSyncTable.InsertAsync 方法,该方法不会提供“参数”字典的重载。 MobileServiceSyncContextExtensions.PushAsync 方法似乎也没有提供将自定义参数添加到查询字符串的方法。

是否有人知道在使用移动服务同步上下文以发送 Azure API 管理服务的订阅密钥时包含自定义参数的方法?

【问题讨论】:

    标签: c# azure windows-runtime azure-mobile-services win-universal-app


    【解决方案1】:

    我已经找到了方法。

    我实现了以下 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")
        );
    

    我希望这对面临同样问题的人有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多