2
votes

I am trying to receive data from Azure Event Hub and display them on .NET core 2.1 Web App using signalR. I have follow this tutorial https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-receive-eph and everything works on .net core console app. But I have stuck on how to implement this in web app. I am still new in web app so maybe my problem is trivial. Here is main web app code

        public static void Main(string[] args)
    {

        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

And part of code from tutorial which is responsible for deploying event processor is in MainAsync(string[] args)

        private static async Task MainAsync(string[] args)
    {
        Console.WriteLine("Registering EventProcessor...");
        var eventProcessorHost = new EventProcessorHost(
            EventHubName,
            PartitionReceiver.DefaultConsumerGroupName,
            EventHubConnectionString,
            StorageConnectionString,
            StorageContainerName);

        // Registers the Event Processor Host and starts receiving messages
        await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();

        Console.WriteLine("Receiving. Press ENTER to stop worker.");
        Console.ReadLine();

        // Disposes of the Event Processor Host
        await eventProcessorHost.UnregisterEventProcessorAsync();
    }

Here is my problem. How to run MainAsync and run web app? If I do something like this

        public static void Main(string[] args)
    {
        MainAsync(args);
        CreateWebHostBuilder(args).Build().Run();

    }

Everything compiles but nothing happens when there is new event in event hub, and I don't know why and how to solve this problem

1

1 Answers

2
votes

It's different in case of web application. There are couple of ways to do this.

1) You create a web job if you are hosting your application in Azure App service.

2) You leverage azure event grid. You need to create the web hook (controller) in your .Net Core that will receive events. Event hub send events to web hook (event hub uses event grid to do that). You create web hook in event hub and provide your controller url there.

3)Your application subscribe to events. I haven't seen concrete example of this but this example may give you hint. https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/subscribe-events