我要在C#控制台应用程序中订阅Azure事件网格,并且该应用程序未托管在Azure上(将在Windows服务器上)。
其他C#webapi项目将触发将在Azure上托管的Azure事件,但是如何在未托管在Azure上的VM上订阅(监听)事件?
在上述情况下我应该选择哪个端点详细信息?
答案 0 :(得分:0)
一个想法是通过将一个端点设置为事件网格(如您的图片)中的专用EH来将事件推送到事件中心。然后,您可以在正在从专用端点读取所有事件的VM上实现侦听器。
下面是脚本外观的小代码示例,从本质上讲,它只是一个小型控制台应用程序,它从事件中心读取事件并将其写入控制台:
public class MyEventProcessor : IEventProcessor
{
private Stopwatch checkpointStopWatch;
public Task ProcessErrorAsync(PartitionContext context, Exception error)
{
Console.WriteLine(error.ToString());
return Task.FromResult(true);
}
async Task IEventProcessor.CloseAsync(PartitionContext context, CloseReason reason)
{
if (reason == CloseReason.Shutdown)
{
await context.CheckpointAsync();
}
}
Task IEventProcessor.OpenAsync(PartitionContext context)
{
var eventHubPartitionId = context.PartitionId;
Console.WriteLine($"Registered reading from the partition: {eventHubPartitionId} ");
this.checkpointStopWatch = new Stopwatch();
this.checkpointStopWatch.Start();
return Task.FromResult<object>(null);
}
//Data comes in here
async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (var eventData in messages)
{
var data = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
Console.WriteLine($"Message Received from partition {context.PartitionId}: {data}");
}
await context.CheckpointAsync();
}
}
class Program
{
static void Main(string[] args)
{
string processorHostName = Guid.NewGuid().ToString();
var Options = new EventProcessorOptions()
{
MaxBatchSize = 1,
};
Options.SetExceptionHandler((ex) =>
{
System.Diagnostics.Debug.WriteLine($"Exception : {ex}");
});
var eventHubCS = "event hub connection string";
var storageCS = "storage connection string";
var containerName = "test";
var eventHubname = "test2";
EventProcessorHost eventProcessorHost = new EventProcessorHost(eventHubname, "$Default", eventHubCS, storageCS, containerName);
eventProcessorHost.RegisterEventProcessorAsync<MyEventProcessor>(Options).Wait();
while(true)
{
//do nothing
}
}
}