Skip to content

daishisystems/Daishi.Pluralsight.EventHub.Demo

Repository files navigation

Image of insidethecpu

Getting Started with Azure Event Hubs with C#

Join the chat at https://gitter.im/Daishi-Pluralsight-EventHub-Demo Build status Course Image

Overview

This repository contains supplemental material to compliment my Pluralsight course Getting Started with Azure Event Hubs with C#. This includes a custom C# library, designed to interface with Event Hubs, a series of Unit Tests, and a sample Console Application.

Connecting to an Event Hub

var eventHubConnectionString =
ConfigurationManager.AppSettings["EventHubConnectionString"];

EventHubToolbox.Instance.Connect(eventHubConnectionString);

if (EventHubToolbox.Instance.IsConnected)
{
    Console.WriteLine(@"Connected OK!");
}

Publishing Events

await EventHubToolbox.Instance.SendAsync("TEST");

Subscribing to an Event Hub

var eventReceiver = new EventReceiver(TimeSpan.FromMinutes(5));
eventReceiver.Notification += EventReceiver_Notification;
eventReceiver.EventReceived += EventReceiverEventReceived;

var eventProcessorOptions = new EventProcessorOptions();
eventProcessorOptions.ExceptionReceived += EventProcessorOptions_ExceptionReceived;

await EventHubToolbox.Instance.SubscribeAsync(
    "MyEventHost",
    eventHubConnectionStringShort,
    eventHubName,
    storageAccountName,
    storageAccountKey,
    eventReceiver,
    EventHubToolbox.UnRegisterAsync,
    EventHubToolbox.RegisterAsync,
    false,
    eventProcessorOptions);

if (EventHubToolbox.Instance.IsSubscribedToAny)
{
    Console.WriteLine(@"Subscribed!");
}

private static void EventReceiver_Notification(object sender,
    EventReceiverEventArgs e)
{
    Console.WriteLine(@"Notification received: {0}", e.Message);
}

private static void EventReceiverEventReceived(object sender,
    EventReceiverEventArgs e)
{
    Console.WriteLine(@"Event received: {0}", e.Message);
}

private static void EventProcessorOptions_ExceptionReceived(
    object sender,
    ExceptionReceivedEventArgs e)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine(e.Exception.Message);
    Console.ForegroundColor = ConsoleColor.Green;
}

Unsubscribe from an Event hub

await EventHubToolbox.Instance.UnsubscribeAllAsync(EventHubToolbox.UnRegisterAsync);

Contact the Developer

Please reach out and contact me for questions, suggestions, or to just talk tech in general.

RSSTwitterLinkedInGoogle+YouTube

About

Getting Started with Azure Event Hub with C# course material.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages