Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure应用程序服务无法从Azure IoT中心获取数据_C#_Azure_Azure Web App Service_Azure Iot Hub - Fatal编程技术网

C# Azure应用程序服务无法从Azure IoT中心获取数据

C# Azure应用程序服务无法从Azure IoT中心获取数据,c#,azure,azure-web-app-service,azure-iot-hub,C#,Azure,Azure Web App Service,Azure Iot Hub,我想使用Azure应用程序服务从Azure物联网中心获取数据 我尝试在Application_Start()函数中将回调事件注册到IoT Hub using Microsoft.ServiceBus.Messaging; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Threa

我想使用Azure应用程序服务从Azure物联网中心获取数据

我尝试在Application_Start()函数中将回调事件注册到IoT Hub



    using Microsoft.ServiceBus.Messaging;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using TelemetryEPHostConsoleApp;

    namespace MCM100_Dashboard.App_Start.TelemetryProcessor
    {
        public class TelemetryMain
        {
            private const string STORAGEACCOUNT_PROTOCOL = "https";// We use HTTPS to access the storage account

            public async static void Main()
            {
                var mainTask = new Task(GetAzureData);
                mainTask.Start();
                await mainTask;
            }
            public static string GetAzureData()
            {
               // IoT Hub
                string iotHubConnectionString = ConfigurationManager.AppSettings["IoTHub.ConnectionString"];
                string eventHubPath = "messages/events";// It's hard-coded for IoT Hub
                string consumerGroupName = "mcmpush";// It's hard-coded for this workshop

                // Storage Account
                string storageAccountName = ConfigurationManager.AppSettings["StorageAccount.Name"];
                string storageAccountKey = ConfigurationManager.AppSettings["StorageAccount.Key"];
                string storageAccountConnectionString = CombineConnectionString(storageAccountName, storageAccountKey);
                string eventProcessorHostName = "eventprocessorhost";
                string leaseName = eventProcessorHostName;

                EventProcessorHost eventProcessorHost = new EventProcessorHost(
                    eventProcessorHostName,
                    eventHubPath,
                    consumerGroupName,
                    iotHubConnectionString,
                    storageAccountConnectionString,
                    leaseName);


                var options = new EventProcessorOptions
                {
                    InitialOffsetProvider = (partitionId) => DateTime.UtcNow
                };
                options.ExceptionReceived += (sender, e) => { Console.WriteLine(e.Exception); };
                re: try
                {
                    eventProcessorHost.RegisterEventProcessorAsync(options).Wait();
                }
                catch (Exception e)
                {
                    System.Threading.Thread.Sleep(1000);
                    goto re;
                }


                eventProcessorHost.UnregisterEventProcessorAsync().Wait();

                return "";

            }

            private static string CombineConnectionString(string storageAccountName, string storageAccountKey)
            {
                return "DefaultEndpointsProtocol=" + STORAGEACCOUNT_PROTOCOL + ";" +
                    "AccountName=" + storageAccountName + ";" +
                    "AccountKey=" + storageAccountKey;
            }
        }
    }

当我可以在PC上运行Visual Studio 2017程序时,Azure IoT Hub会正常触发事件并获取数据

不幸的是,当我部署到Azure云服务“Azure应用程序服务”时,它只能在应用程序服务在几秒钟内启动时从Azure IoT Hub获取触发事件

没有从Azure IoT Hub返回的数据,事件不再触发

我不知道原因。任何建议都将被告知

下面是将在应用程序服务的开头运行的事件触发器代码。 入口点是Main()函数



    using Microsoft.ServiceBus.Messaging;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using TelemetryEPHostConsoleApp;

    namespace MCM100_Dashboard.App_Start.TelemetryProcessor
    {
        public class TelemetryMain
        {
            private const string STORAGEACCOUNT_PROTOCOL = "https";// We use HTTPS to access the storage account

            public async static void Main()
            {
                var mainTask = new Task(GetAzureData);
                mainTask.Start();
                await mainTask;
            }
            public static string GetAzureData()
            {
               // IoT Hub
                string iotHubConnectionString = ConfigurationManager.AppSettings["IoTHub.ConnectionString"];
                string eventHubPath = "messages/events";// It's hard-coded for IoT Hub
                string consumerGroupName = "mcmpush";// It's hard-coded for this workshop

                // Storage Account
                string storageAccountName = ConfigurationManager.AppSettings["StorageAccount.Name"];
                string storageAccountKey = ConfigurationManager.AppSettings["StorageAccount.Key"];
                string storageAccountConnectionString = CombineConnectionString(storageAccountName, storageAccountKey);
                string eventProcessorHostName = "eventprocessorhost";
                string leaseName = eventProcessorHostName;

                EventProcessorHost eventProcessorHost = new EventProcessorHost(
                    eventProcessorHostName,
                    eventHubPath,
                    consumerGroupName,
                    iotHubConnectionString,
                    storageAccountConnectionString,
                    leaseName);


                var options = new EventProcessorOptions
                {
                    InitialOffsetProvider = (partitionId) => DateTime.UtcNow
                };
                options.ExceptionReceived += (sender, e) => { Console.WriteLine(e.Exception); };
                re: try
                {
                    eventProcessorHost.RegisterEventProcessorAsync(options).Wait();
                }
                catch (Exception e)
                {
                    System.Threading.Thread.Sleep(1000);
                    goto re;
                }


                eventProcessorHost.UnregisterEventProcessorAsync().Wait();

                return "";

            }

            private static string CombineConnectionString(string storageAccountName, string storageAccountKey)
            {
                return "DefaultEndpointsProtocol=" + STORAGEACCOUNT_PROTOCOL + ";" +
                    "AccountName=" + storageAccountName + ";" +
                    "AccountKey=" + storageAccountKey;
            }
        }
    }


RegisterEventProcessorAsync
之后立即执行
UnregisterEventProcessorAsync
。为什么呢?处理事件的时间不多。您最好使用连续运行的webjob从IoT中心读取数据

只有当您想停止侦听传入事件时,才应调用
UnregisterEventProcessorAsync
。在注册eventprocessor之后和注销之前,运行时将调用
任务IEventProcessor.ProcessEventsSync(PartitionContext,IEnumerable messages)
任意次数


还有,
return''是怎么回事我见过几次?

这方面有什么进展吗?是的,非常感谢。我们在其他服务中发现了另一个问题。故障问题是由于另一个服务导致的,因此我无法识别错误。非常感谢。这个问题的解决方案是解决另一个将数据推送到Azure IoTHub的服务。我们发现它会在几分钟后断开连接。我在本地主机上看到数据而在云端看不到数据的原因是“时间延迟”。当数据发送到IoTHub时,请在本地主机上设置时间延迟,以便在Azure IoTHub服务崩溃之前查看数据。另一方面,云无法在Azure IoTHub服务崩溃后立即获取数据。基于这些原因,我们看到了这种情况。谢谢。