DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

4 1

C# SDK for Event Hub: Read stream from the latest message

The official documentation explains Azure Event Hub as:

Azure Event Hubs is a big data streaming platform and event ingestion service. It can receive and process millions of events per second

There is C# (and many other) SDK to use the service. I am using dotnet core SDK

Issue

I need to get messages since I subscribed to the event hub, but by default, event hub retains data for 1 day and the sample code gets all the old messages. I can use checkpoint to remember where I last read the message for next time, but I always want to get data from the point I open connection.

Resolution

Sample application shows how to subscribe events.

// Register handlers for processing events and handling errors
processor.ProcessEventAsync += ProcessEventHandler;
processor.ProcessErrorAsync += ProcessErrorHandler;

In addition to above, I just needed to subscribe PartitionInitializingAsync event.

processor.PartitionInitializingAsync+= ProcessInitializingHandler;

static async Task ProcessInitializingHandler(PartitionInitializingEventArgs eventArgs)
{
    eventArgs.DefaultStartingPosition = EventPosition.Latest;
}

That's it! You can also set FromOffset, FromEnqueuedTime for more granular control.

Reference

You can find official sample in GitHub

đź‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay