Azure WebJob无法从PostAsync调用获得响应

时间:2018-05-01 05:59:51

标签: azure azure-webjobs

我已经使用Windows服务,将其修改为控制台应用程序,然后将其作为WebJob发布。当它执行PostAsync命令时,它没有响应,并且不会在PostAsync命令之后立即执行Console.WriteLine。

以下是代码:

            Console.WriteLine("Querying for access credentials...");
            HttpResponseMessage responsePut = null;
            try
            {
                Console.WriteLine("PostAsync call");
                responsePut = await client.PostAsync("/oauth/access_token", new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded"));
                Console.WriteLine("Returned from PostAsync call");
            }
            catch (System.Net.Http.HttpRequestException err)
            {
                Console.WriteLine("Unable to connect to the server (HttpRequestException)..." + err.InnerException);
                throw new Exception("Unable to connect to the server (HttpRequestException)..." + err.InnerException);
            }
            catch (Exception err)
            {
                Console.WriteLine("Unable to connect to the server (Exception)..." + err.Message + "::" + err.InnerException);
                throw new Exception("Unable to connect to the server (Exception)..." + err.InnerException);
            }

Azure控制台日志如下:

[05/01/2018 05:45:31 > e559bd: INFO] Querying for access credentials...
[05/01/2018 05:45:31 > e559bd: INFO] PostAsync call
[05/01/2018 05:45:32 > e559bd: SYS INFO] Status changed to Success
[05/01/2018 05:45:32 > e559bd: SYS INFO] Process went down, waiting for 60 seconds

此代码用作Windows服务和Windows控制台应用程序,但在Azure上失败。

我可以做些什么来确定Azure对我做了什么和/或以不同方式执行此操作的建议。

1 个答案:

答案 0 :(得分:0)

我创建了一张支持票,并得到了相当快的响应。

包含PostAsync的代码行必须按如下方式修改:

responsePut = client.PostAsync("/oauth/access_token", new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded")).Result;

请注意删除await并添加.Result

这被认为是语法错误',我不同意这一点!但它解决了这个问题。