C# IEventProcessor未从事件中心读取
我目前正在使用EventProcessorHost和一个简单的IEventProcessor实现来实现一个事件中心读取器。我已经确认,遥测数据正在使用保罗·萨尔瓦托里的优秀技术写入活动中心。我已成功配置EventProcessorHost,以便将存储帐户用于租约和检查点。我可以在存储帐户中看到事件中心数据文件。我现在看到的问题是IEventProcessor实现似乎没有从事件中心读取任何内容 我没有收到任何例外。测试控制台应用程序正在连接到存储帐户,没有问题。我注意到,我添加到构造函数中的日志语句从未被调用,因此看起来似乎从未真正创建过接收方。我觉得我错过了一些简单的事情。谁能帮我确定我错过了什么?谢谢大家! IEventProcessor实现:C# IEventProcessor未从事件中心读取,c#,azure-eventhub,C#,Azure Eventhub,我目前正在使用EventProcessorHost和一个简单的IEventProcessor实现来实现一个事件中心读取器。我已经确认,遥测数据正在使用保罗·萨尔瓦托里的优秀技术写入活动中心。我已成功配置EventProcessorHost,以便将存储帐户用于租约和检查点。我可以在存储帐户中看到事件中心数据文件。我现在看到的问题是IEventProcessor实现似乎没有从事件中心读取任何内容 我没有收到任何例外。测试控制台应用程序正在连接到存储帐户,没有问题。我注意到,我添加到构造函数中的日志语
namespace Receiver
{
internal class SimpleEventProcessor : IEventProcessor
{
private Stopwatch _checkPointStopwatch;
public SimpleEventProcessor()
{
Console.WriteLine("SimpleEventProcessor created");
}
#region Implementation of IEventProcessor
public Task OpenAsync(PartitionContext context)
{
Console.WriteLine("SimpleEventProcessor initialized. Partition: '{0}', Offset: '{1}",
context.Lease.PartitionId, context.Lease.Offset);
_checkPointStopwatch = new Stopwatch();
_checkPointStopwatch.Start();
return Task.FromResult<object>(null);
}
public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (var data in messages.Select(eventData => Encoding.UTF8.GetString(eventData.GetBytes())))
{
Console.WriteLine("Message received. Partition: '{0}', Data: '{1}'", context.Lease.PartitionId,
data);
}
if (_checkPointStopwatch.Elapsed > TimeSpan.FromSeconds(30))
{
await context.CheckpointAsync();
_checkPointStopwatch.Restart();
}
}
public async Task 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();
}
}
#endregion
}
}
名称空间接收器
{
内部类SimpleEventProcessor:IEventProcessor
{
私人秒表(秒表);;
公共SimpleEventProcessor()
{
Console.WriteLine(“SimpleEventProcessor已创建”);
}
#IEventProcessor的区域实现
公共任务OpenAsync(PartitionContext上下文)
{
WriteLine(“SimpleEventProcessor已初始化。分区:'{0}',偏移量:'{1}”,
context.Lease.PartitionId、context.Lease.Offset);
_checkPointStopwatch=新秒表();
_checkPointStopwatch.Start();
返回Task.FromResult(空);
}
公共异步任务进程EventsAsync(PartitionContext上下文,IEnumerable消息)
{
foreach(messages.Select中的var数据(eventData=>Encoding.UTF8.GetString(eventData.GetBytes()))
{
WriteLine(“收到的消息.分区:'{0}',数据:'{1}',context.Lease.PartitionId,
数据);
}
如果(_checkPointStopwatch.appeased>TimeSpan.FromSeconds(30))
{
wait context.CheckpointAsync();
_checkPointStopwatch.Restart();
}
}
公共异步任务CloseAsync(PartitionContext上下文,CloseReason)
{
WriteLine(“处理器正在关闭。分区“{0}”,原因:{1}”,context.Lease.PartitionId,
原因);
如果(原因==CloseReason.Shutdown)
{
wait context.CheckpointAsync();
}
}
#端区
}
}
测试控制台代码:
namespace EventHubTestConsole
{
internal class Program
{
private static void Main(string[] args)
{
AsyncPump.Run((Func<Task>) MainAsync);
}
private static async Task MainAsync()
{
const string eventHubConnectionString =
"Endpoint=<EH endpoint>;SharedAccessKeyName=<key name>;SharedAccessKey=<key>";
const string eventHubName = "<event hub name>";
const string storageAccountName = "<storage account name>";
const string storageAccountKey = "<valid storage key>";
var storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
storageAccountName, storageAccountKey);
Console.WriteLine("Connecting to storage account with ConnectionString: {0}", storageConnectionString);
var eventProcessorHostName = Guid.NewGuid().ToString();
var eventProcessorHost = new EventProcessorHost(
eventProcessorHostName,
eventHubName,
EventHubConsumerGroup.DefaultGroupName,
eventHubConnectionString,
storageConnectionString);
var epo = new EventProcessorOptions
{
MaxBatchSize = 100,
PrefetchCount = 1,
ReceiveTimeOut = TimeSpan.FromSeconds(20),
InitialOffsetProvider = (name) => DateTime.Now.AddDays(-7)
};
epo.ExceptionReceived += OnExceptionReceived;
await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>(epo);
Console.WriteLine("Receiving. Please enter to stop worker.");
Console.ReadLine();
}
public static void OnExceptionReceived(object sender, ExceptionReceivedEventArgs args)
{
Console.WriteLine("Event Hub exception received: {0}", args.Exception.Message);
}
}
命名空间EventHubTestConsole
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
AsyncPump.Run((Func)mainSync);
}
专用静态异步任务mainsync()
{
常量字符串eventHubConnectionString=
“Endpoint=;SharedAccessKeyName=;SharedAccessKey=”;
常量字符串eventHubName=“”;
常量字符串storageAccountName=“”;
常量字符串storageAccountKey=“”;
var storageConnectionString=string.Format(“DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}”,
storageAccountName、storageAccountKey);
WriteLine(“使用ConnectionString:{0}连接到存储帐户”,storageConnectionString);
var eventProcessorHostName=Guid.NewGuid().ToString();
var eventProcessorHost=新的eventProcessorHost(
eventProcessorHostName,
事件名称,
EventHubConsumerGroup.DefaultGroupName,
EventHub连接字符串,
存储连接字符串);
var epo=新事件处理器选项
{
MaxBatchSize=100,
预取计数=1,
ReceiveTimeOut=TimeSpan.FromSeconds(20),
InitialOffsetProvider=(名称)=>DateTime.Now.AddDays(-7)
};
epo.ExceptionReceived+=OneExceptionReceived;
等待eventProcessorHost.RegisterEventProcessorAsync(epo);
Console.WriteLine(“接收。请输入以停止工作人员”);
Console.ReadLine();
}
公共静态void OneExceptionReceived(对象发送方,ExceptionReceivedEventArgs参数)
{
WriteLine(“接收到事件中心异常:{0}”,args.exception.Message);
}
}
问题似乎出在事件处理器选项的值上。预回迁计数
我稍微更改了您的代码,如图所示(删除异步泵并完全关闭接收器)。我发现RegisterEventProcessorAsync在预取计数小于10时引发异常
namespace EventHubTestConsole
{
internal class Program
{
private static void Main(string[] args)
{
const string eventHubConnectionString =
"Endpoint=<EH endpoint>;SharedAccessKeyName=<key name>;SharedAccessKey=<key>";
const string eventHubName = "<event hub name>";
const string storageAccountName = "<storage account name>";
const string storageAccountKey = "<valid storage key>";
var storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
storageAccountName, storageAccountKey);
Console.WriteLine("Connecting to storage account with ConnectionString: {0}", storageConnectionString);
var eventProcessorHostName = Guid.NewGuid().ToString();
var eventProcessorHost = new EventProcessorHost(
eventProcessorHostName,
eventHubName,
EventHubConsumerGroup.DefaultGroupName,
eventHubConnectionString,
storageConnectionString);
var epo = new EventProcessorOptions
{
MaxBatchSize = 100,
PrefetchCount = 10,
ReceiveTimeOut = TimeSpan.FromSeconds(20),
InitialOffsetProvider = (name) => DateTime.Now.AddDays(-7)
};
epo.ExceptionReceived += OnExceptionReceived;
eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>(epo).Wait();
Console.WriteLine("Receiving. Please enter to stop worker.");
Console.ReadLine();
eventProcessorHost.UnregisterEventProcessorAsync().Wait();
}
public static void OnExceptionReceived(object sender, ExceptionReceivedEventArgs args)
{
Console.WriteLine("Event Hub exception received: {0}", args.Exception.Message);
}
}
}
命名空间EventHubTestConsole
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
常量字符串eventHubConnectionString=
“Endpoint=;SharedAccessKeyName=;SharedAccessKey=”;
常量字符串eventHubName=“”;
常量字符串storageAccountName=“”;
常量字符串storageAccountKey=“”;
var storageConnectionString=string.Format(“DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}”,
storageAccountName、storageAccountKey);
WriteLine(“使用ConnectionString:{0}连接到存储帐户”,storageConnectionString);
var eventProcessorHostName=Guid.NewGuid().ToString();
var eventProcessorHost=新的eventProcessorHost(
eventProcessorHostName,
事件名称,
EventHubConsumerGroup.DefaultGroupName,
EventHub连接字符串,
存储连接字符串);
var epo=新事件处理器选项
{
MaxBatchSize=100,
预取计数=10,
ReceiveTimeOut=TimeSpan.FromSeconds(20),
InitialOffsetProvider=(名称)=>DateTime.Now.AddDay