我想使用Azure App Service从Azure IoT中心获取数据。
我尝试在Application_Start()函数中向IoT中心注册回调事件。
当我可以在PC上使用Visual Studio 2017运行程序时,通常由Azure IoT中心触发事件并获取数据。
不幸的是,当我部署到作为“ Azure App Service”服务的Azure云时,当App Service在几秒钟内启动时,它只能从Azure IoT中心获取触发事件。
没有数据从Azure IoT中心获取,并且该事件不再触发。
我不知道原因。任何建议都将适用。
这是事件触发代码将在App Service的开头运行。 入口点是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;
}
}
}
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Configuration;
using System.IO;
using System.Web;
namespace TelemetryEPHostConsoleApp
{
class TelemetryEventProcessor :IEventProcessor
{
static WebServerConnector _webSC = new WebServerConnector();
async Task IEventProcessor.CloseAsync(PartitionContext context, CloseReason reason)
{
if (reason == CloseReason.Shutdown)
{
await context.CheckpointAsync();
}
}
Task IEventProcessor.OpenAsync(PartitionContext context)
{
return Task.FromResult(null);
}
async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable messages)
{
// hope to trigger event
foreach (EventData eventData in messages)
{
string data = Encoding.UTF8.GetString(eventData.GetBytes());
}
//Call checkpoint every 5 minutes, so that worker can resume processing from 5 minutes back if it restarts.
if (this.checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(5))
{
await context.CheckpointAsync();
this.checkpointStopWatch.Restart();
}
}
private void ProcessMessage(string data)
{
return "";
}
}
}
答案 0 :(得分:0)
在RegisterEventProcessorAsync
之后立即执行UnregisterEventProcessorAsync
。这是为什么?您没有太多时间来处理事件。使用连续运行的Web作业从IoT中心读取数据可能会更好。
仅当您想停止监听传入事件时才应呼叫UnregisterEventProcessorAsync
。注册事件处理器之后且注销之前,运行时将调用Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable messages)
任意次。
还有,我见过几次return "";
了吗?