从 Azure Web App 发送到 Azure IoT 中心设备时消息挂起(超时)

Message hangs (timeout) when sending from Azure Web App to Azure IoT Hub Devices

下面我展示了处理设备通知逻辑的 Azure Web 应用程序的一部分。我正在做的是从 ASP MVC 控制器调用下面显示的代码。 但是当我 运行 它时,我从浏览器收到一个挂起的(永远挂起的)请求。

我想我已经通过将 SendAsync 调用包装在 BackgroundWorker 线程中找到了解决方法。好多了,但我工作不正常。前几次(一两次)它工作正常,但随后再次发生,包裹的线程挂起。

该代码与 MSDN 上的控制台应用程序代码相差无几。我错过了什么?

using System.Web.Configuration;
using Microsoft.Azure.Devices;

namespace MyTest
{
    public class Sender
    {
        private readonly string connectionString;
        private readonly Microsoft.Azure.Devices.ServiceClient serviceClient;

        public Sender()
        {
            connectionString = WebConfigurationManager.AppSettings["ConnectionString"];
            serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
        }

        public async void SendRequest(string deviceId, string msgText)
        {
            var message = new Message();
            message.Properties.Add("text", msgText));

            await serviceClient.SendAsync(deviceId, message);
        }
    }
}

问题是由于 ASP MVC 框架使用不当造成的。

事实证明,当使用长 运行 async\await 时,必须使用 AsyncController 而不是仅使用 Controller。管道必须一直异步。