/Daishi.Pluralsight.EventHub.Demo

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

Primary LanguageC#GNU General Public License v3.0GPL-3.0

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