从MVC Web应用程序发送到Azure IoT Hub设备时,应用程序挂起

时间:2017-09-05 19:12:40

标签: azure asp.net-mvc-4 azure-web-sites iot azure-iot-hub

这是处理设备通知逻辑的Azure Web App的控制器代码。我正在做的是从ASP MVC控制器调用下面显示的代码。但是当我运行它时,我从浏览器获得了一个待处理的请求(它挂起)。 我在视图上有一个按钮,当单击它时,它会在控制器中调用Wakeup方法。

对于控制台应用程序,代码与MSDN上的代码没有区别。我错过了什么?

using System.Text;
using Microsoft.Azure.Devices;

 public class MyTemplate2Controller : Controller
    {
        static ServiceClient serviceClient;
        static string connectionString = "HostName=azure-devices.net;SharedAccessKeyName=iothub;SharedAccessKey=mrUPt*2318C18K3LUk+oFarkNQ4vRvHrOa/eg=";

        private AsyncController Task SendCloudToDeviceMessageAsync()
        {
            var asd = "{13A20041677B0,4,15,0}";
            var commandMessage = new Message(Encoding.ASCII.GetBytes(asd));
          return  await serviceClient.SendAsync("Test_Comp_Dev_1", commandMessage).ConfigureAwait(continueOnCapturedContext: false);
        }
        public void Wakeup()
        {

            serviceClient = ServiceClient.CreateFromConnectionString(connectionString);

            SendCloudToDeviceMessageAsync().Wait();
        }

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

    public void Wakeup()
    {
        serviceClient = ServiceClient.CreateFromConnectionString(connectionString);

        System.Threading.ThreadPool.QueueUserWorkItem(a => SendCloudToDeviceMessageAsync().Wait());
    }