无法在web应用程序中执行receieveevent()和RegisterEventProcessorAsync,无法使
本文关键字:RegisterEventProcessorAsync 执行 web 应用程序 receieveevent | 更新日期: 2023-09-27 18:17:47
控制台应用程序与Azure Event Hub
完美配合,但当我尝试将RecieveEvent()
代码在Index.aspx.cs
里面它得到了一个调用,但是在此之后它执行失败了
eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>(options).Wait(); and `SimpleEventProcessor.cs` code why so ?
如何在web应用中做到这一点?
Index.aspx.cs
[WebMethod]
public static void RecieveEvent()
{
string eventHubConnectionString = "Endpoint=sb://rpidemoeventhub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=wyi1HpKwEIOpiSPnQaCloPr9ELhESOSD/F2SJiY0RFU=";
string eventHubName = "myeventhubname";
string storageAccountName = "mystoragename";
string storageAccountKey = "mykey";
string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccountName, storageAccountKey);
string eventProcessorHostName = Guid.NewGuid().ToString();
EventProcessorHost eventProcessorHost = new EventProcessorHost(eventProcessorHostName, eventHubName, EventHubConsumerGroup.DefaultGroupName, eventHubConnectionString, storageConnectionString);
//Console.WriteLine("Registering EventProcessor...");
var options = new EventProcessorOptions();
options.ExceptionReceived += (sender, e) => { Console.WriteLine(e.Exception); };
eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>(options).Wait();
//Console.WriteLine("Receiving. Press enter key to stop worker.");
//Console.ReadLine();
eventProcessorHost.UnregisterEventProcessorAsync().Wait();
}
SimpleEventProcessor
public class SimpleEventProcessor : IEventProcessor
{
Stopwatch checkpointStopWatch;
async Task IEventProcessor.CloseAsync(PartitionContext context, CloseReason reason)
{
// Console.WriteLine("Processor Shutting Down. Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason);
if (reason == CloseReason.Shutdown)
{
await context.CheckpointAsync();
}
}
Task IEventProcessor.OpenAsync(PartitionContext context)
{
// Console.WriteLine("SimpleEventProcessor initialized. Partition: '{0}', Offset: '{1}'", context.Lease.PartitionId, context.Lease.Offset);
this.checkpointStopWatch = new Stopwatch();
this.checkpointStopWatch.Start();
return Task.FromResult<object>(null);
}
async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (EventData eventData in messages)
{
string data = Encoding.UTF8.GetString(eventData.GetBytes());
//TODO: sed mail notification
if (Convert.ToInt32(data) > 25 && Index.isMail == false)
{
SendMail();
Index.isMail = true;
}
//Console.WriteLine(string.Format("Message received. Partition: '{0}', Data: '{1}'",
// context.Lease.PartitionId, data));
}
//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();
}
}
无法执行上面的调用,为什么调用不来这里?而不是使用任何工人角色或webjob可以工作吗?
除了p p的第一个解释为什么它不起作用的注释之外,考虑如下:
您应该看到EventProcessor
的目的是在连续的进程/循环中将传入的事件流读取到EventHub
(在EventProcessor
实例启动并运行时调用ProcessEventsAsync
方法)。这就是为什么web job/worker角色更适合于此,而不是在web应用程序页面中这样做,因为这意味着在合理的时间内处理请求。
如果你真的想让它在web应用程序页面中运行,不要使用EventProcessor
,而是创建一个直接接收器(https://azure.microsoft.com/en-us/documentation/articles/event-hubs-programming-guide/#event-consumers),并使用Receive(In32)
方法指定你想要接收的消息数量:https://msdn.microsoft.com/en-us/library/azure/dn790451.aspx。这样就可以处理和显示有限数量的消息。
我不知道你的具体用例,但我们在一个连续运行的web作业中托管EventProcessor
,当web作业由于停止命令或其他原因关闭时,我们可以通过调用UnregisterEventProcessorAsync()
来优雅地停止EventProcessor
。