0

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?

enter image description here

Yogen Darji
  • 3,230
  • 16
  • 31
  • any specific reason for having a listener on a vm or outside azure ? this sounds like a good use case for using azure functions. – Aravind Nov 28 '19 at 09:18
  • @Aravind I've one C++ library to run from .NET console application so I've to use VM over Azure. – Yogen Darji Nov 29 '19 at 04:58
  • did you check this already https://stackoverflow.com/questions/53643543/include-c-c-unmanaged-code-dll-consumed-using-dllimport-in-azure-functions-p/53643896 – Aravind Nov 29 '19 at 05:57

1 Answers1

0

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
        }
    }
}
kgalic
  • 2,441
  • 1
  • 9
  • 21