【问题标题】:Azure WebJob does not get response from PostAsync callAzure WebJob 未从 PostAsync 调用中获得响应
【发布时间】:2018-05-01 05:59:50
【问题描述】:

我采用了 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 对我做了什么和/或建议以不同的方式做这件事。

【问题讨论】:

标签: azure azure-webjobs


【解决方案1】:

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

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

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

注意删除await 并添加.Result

这被认为是“语法错误”,我不同意!但它解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多