简体   繁体   中英

Azure Event Grid subscription to Console Application on VM

I want to Subscribe for Azure Event Grid in C# Console Application and that application is not hosted on Azure(will be on Windows server).

Other C# webapi project will fire Azure Event that will be hosted on Azure but how to Subscribe(listen) for Event on VM that is not hosted on Azure?

Which Endpoint detail should I select for above scenario?

在此处输入图像描述

One idea would be to push the event to the event hub by setting an endpoint to the dedicated EH from the event grid(like from your picture). Then you can implement the listener on your VM that is reading all the events from the dedicated endpoint.

Below is the small code sample of how your script should look like, and essentially it is just a small console application that reads the events from the event hub and writes to the console:

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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM