【问题标题】:Typed result from Azure mobile service with custom headers使用自定义标头从 Azure 移动服务输入的结果
【发布时间】:2016-01-05 12:44:59
【问题描述】:

在调用 Azure 移动服务时,我想向自定义 REST 服务发出请求,并将我的自定义标头值一起传递并返回输入的结果。为什么我没有那个选项?我错过了什么?

查看https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.mobileservices.mobileserviceclient.invokeapiasync.aspx 我只能在发出无类型请求时提供自定义标头。

【问题讨论】:

    标签: .net rest azure azure-mobile-services


    【解决方案1】:

    由于对称性,没有添加需要额外标头的重载:在类型化场景中,您可以为请求指定标头,但无法读取响应中的标头(因为返回类型是类型 T,它不包含标头集合)。

    但是,您可以使用可以传递给MobileServiceClient 构造函数的消息处理程序来执行此操作的扩展方法。下面的代码是这种实现的一个例子。它只实现了两个类型化的方法,但如果你想添加其余的方法,实现是微不足道的。

    class Program
    {
        public static MobileServiceClient MobileService = new MobileServiceClient(
            "https://YOUR-SERVICE.azure-mobile.net/",
            "YOUR-APPLICATION-KEY"
        );
    
        static void Main(string[] args)
        {
            DoWork().Wait();
        }
    
        static async Task DoWork()
        {
            var httpHeaders = new Dictionary<string, string>
            {
                { "x-header-1", "value 1" },
                { "x-header-2", "value 2" },
            };
            var test = await MobileService.InvokeApiWithHeaders<Test>("headers", httpHeaders);
            Console.WriteLine("Returned by the service:");
            foreach (var k in test.AllHeaderValues.Keys)
            {
                Console.WriteLine("  {0}: {1}", k, test.AllHeaderValues[k]);
            }
        }
    }
    
    public class Test
    {
        public Dictionary<string, string> AllHeaderValues { get; set; }
    }
    
    public static class TypedInvokeApiWithHeadersExtensions
    {
        public static Task<T> InvokeApiWithHeaders<T>(this MobileServiceClient client, string apiName, IDictionary<string, string> httpHeaders)
        {
            var client2 = new MobileServiceClient(client.ApplicationUri, client.ApplicationKey, new AddHeadersHandler(httpHeaders));
            return client2.InvokeApiAsync<T>(apiName);
        }
    
        public static Task<T> InvokeApiWithHeaders<T>(this MobileServiceClient client, string apiName, HttpMethod method, IDictionary<string, string> httpHeaders, IDictionary<string, string> queryParameters)
        {
            var client2 = new MobileServiceClient(client.ApplicationUri, client.ApplicationKey, new AddHeadersHandler(httpHeaders));
            return client2.InvokeApiAsync<T>(apiName, method, queryParameters);
        }
    
        class AddHeadersHandler : DelegatingHandler
        {
            IDictionary<string, string> headers;
            public AddHeadersHandler(IDictionary<string, string> headers)
            {
                this.headers = headers;
            }
            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
            {
                foreach (var header in headers)
                {
                    request.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }
    
                return base.SendAsync(request, cancellationToken);
            }
        }
    }
    

    【讨论】:

    • 谢谢!很好的实现!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多