从MVC Web应用程序发送到Azure IoT中心设备时应用程序挂起 [英] Application hangs when sending from MVC Web Application to Azure IoT Hub Device

查看:88
本文介绍了从MVC Web应用程序发送到Azure IoT中心设备时应用程序挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a controller code of an Azure Web App that handles a device notification logic. What I'm doing is invoking the code shown below from a ASP MVC Controller. But when I run it I get an ever-pending request from the browser(it hangs). I have a button on view, when clicked, it invokes Wakeup method in the controller.

该代码与

The code is not different from the one on MSDN for a console application. What am I missing?

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();
        }

推荐答案

尝试以下操作:

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

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

这篇关于从MVC Web应用程序发送到Azure IoT中心设备时应用程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆