Microsoft.Azure.WebJobs.Extensions.EventHubs 5.0.0 Prefix Reserved

Install-Package Microsoft.Azure.WebJobs.Extensions.EventHubs -Version 5.0.0
dotnet add package Microsoft.Azure.WebJobs.Extensions.EventHubs --version 5.0.0
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="5.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.Azure.WebJobs.Extensions.EventHubs --version 5.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Microsoft.Azure.WebJobs.Extensions.EventHubs, 5.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Microsoft.Azure.WebJobs.Extensions.EventHubs as a Cake Addin
#addin nuget:?package=Microsoft.Azure.WebJobs.Extensions.EventHubs&version=5.0.0

// Install Microsoft.Azure.WebJobs.Extensions.EventHubs as a Cake Tool
#tool nuget:?package=Microsoft.Azure.WebJobs.Extensions.EventHubs&version=5.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Azure WebJobs Event Hubs client library for .NET

This extension provides functionality for accessing Azure Event Hubs from an Azure Function.

Getting started

Install the package

Install the Event Hubs extension with NuGet:

dotnet add package Microsoft.Azure.WebJobs.Extensions.EventHubs --version 5.0.0-beta.1

Prerequisites

  • Azure Subscription: To use Azure services, including Azure Event Hubs, you'll need a subscription. If you do not have an existing Azure account, you may sign up for a free trial or use your Visual Studio Subscription benefits when you create an account.

  • Event Hubs namespace with an Event Hub: To interact with Azure Event Hubs, you'll also need to have a namespace and Event Hub available. If you are not familiar with creating Azure resources, you may wish to follow the step-by-step guide for creating an Event Hub using the Azure portal. There, you can also find detailed instructions for using the Azure CLI, Azure PowerShell, or Azure Resource Manager (ARM) templates to create an Event Hub.

  • Azure Storage account with blob storage: To persist checkpoints as blobs in Azure Storage, you'll need to have an Azure Storage account with blobs available. If you are not familiar with Azure Storage accounts, you may wish to follow the step-by-step guide for creating a storage account using the Azure portal. There, you can also find detailed instructions for using the Azure CLI, Azure PowerShell, or Azure Resource Manager (ARM) templates to create storage accounts.

Deploy button

Authenticate the Client

For the Event Hubs client library to interact with an Event Hub, it will need to understand how to connect and authorize with it. The easiest means for doing so is to use a connection string, which is created automatically when creating an Event Hubs namespace. If you aren't familiar with using connection strings with Event Hubs, you may wish to follow the step-by-step guide to get an Event Hubs connection string.

The Connection property of EventHubAttribute and EventHubTriggerAttribute is used to specify the configuration property that stores the connection string.

The AzureWebJobsStorage connection string is used to preserve the processing checkpoint information.

For the local development use the local.settings.json file to store the connection string:

{
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "<connection_name>": "Endpoint=sb://<event_hubs_namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=Jya7Eh76HU92ibsxuk1ITN8CM8Bt76YLKf5ISjU3jZ8="
  }
}

When deployed use the application settings to set the connection string.

Managed identity authentication

If your environment has managed identity enabled you can use it to authenticate the Event Hubs extension. Before doing so, you will need to ensure that permissions have been configured as described in the Azure Functions developer guide.

To use managed identity provide the <connection_name>__fullyQualifiedNamespace configuration setting.

{
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "<connection_name>__fullyQualifiedNamespace": "{event_hubs_namespace}.servicebus.windows.net"
  }
}

Or in the case of deployed app set the same setting in application settings:

<connection_name>__fullyQualifiedNamespace={event_hubs_namespace}.servicebus.windows.net

Key concepts

Event Hub Trigger

The Event Hub Trigger allows a function to be executed when a message is sent to an Event Hub.

Please follow the Azure Event Hubs trigger tutorial to learn more about Event Hub triggers.

Event Hub Output Binding

The Event Hub Output Binding allows a function to send Event Hub events.

Please follow the Azure Event Hubs output binding to learn more about Event Hub bindings.

Supported types

The following types are supported for trigger and output bindings:

  • EventData
  • string - value would be encoded using UTF8 encoding
  • BinaryData
  • byte[]
  • Custom model types will be JSON-serialized using Newtonsoft.Json
  • IAsyncCollector<T> of any of the above types for batch triggers
  • EventHubProducerClient for output bindings

Examples

Sending individual event

You can send individual events to an Event Hub by applying the EventHubAttribute the function return value. The return value can be of string or EventData type.

[FunctionName("BindingToReturnValue")]
[return: EventHub("<event_hub_name>", Connection = "<connection_name>")]
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
{
    // This value would get stored in EventHub event body.
    // The string would be UTF8 encoded
    return $"C# Timer trigger function executed at: {DateTime.Now}";
}

Sending multiple events

To send multiple events from a single Azure Function invocation you can apply the EventHubAttribute to the IAsyncCollector<string> or IAsyncCollector<EventData> parameter.

[FunctionName("BindingToCollector")]
public static async Task Run(
    [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
    [EventHub("<event_hub_name>", Connection = "<connection_name>")] IAsyncCollector<EventData> collector)
{
    // IAsyncCollector allows sending multiple events in a single function invocation
    await collector.AddAsync(new EventData(new BinaryData($"Event 1 added at: {DateTime.Now}")));
    await collector.AddAsync(new EventData(new BinaryData($"Event 2 added at: {DateTime.Now}")));
}

Using binding to strongly-typed models

To use strongly-typed model classes with the EventHub binding apply the EventHubAttribute to the model parameter.

[FunctionName("TriggerSingleModel")]
public static void Run(
    [EventHubTrigger("<event_hub_name>", Connection = "<connection_name>")] Dog dog,
    ILogger logger)
{
    logger.LogInformation($"Who's a good dog? {dog.Name} is!");
}

Sending multiple events using EventHubProducerClient

You can also bind to the EventHubProducerClient directly to have the most control over the event sending.

[FunctionName("BindingToProducerClient")]
public static async Task Run(
    [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
    [EventHub("<event_hub_name>", Connection = "<connection_name>")] EventHubProducerClient eventHubProducerClient)
{
    // IAsyncCollector allows sending multiple events in a single function invocation
    await eventHubProducerClient.SendAsync(new[]
    {
        new EventData(new BinaryData($"Event 1 added at: {DateTime.Now}")),
        new EventData(new BinaryData($"Event 2 added at: {DateTime.Now}"))
    });
}

Per-event triggers

To run a function every time an event is sent to Event Hub apply the EventHubTriggerAttribute to a string or EventData parameter.

[FunctionName("TriggerSingle")]
public static void Run(
    [EventHubTrigger("<event_hub_name>", Connection = "<connection_name>")] string eventBodyAsString,
    ILogger logger)
{
    logger.LogInformation($"C# function triggered to process a message: {eventBodyAsString}");
}

Batch triggers

To run a function for a batch of received events apply the EventHubTriggerAttribute to a string[] or EventData[] parameter.

[FunctionName("TriggerBatch")]
public static void Run(
    [EventHubTrigger("<event_hub_name>", Connection = "<connection_name>")] EventData[] events,
    ILogger logger)
{
    foreach (var e in events)
    {
        logger.LogInformation($"C# function triggered to process a message: {e.EventBody}");
        logger.LogInformation($"EnqueuedTime={e.EnqueuedTime}");
    }
}

Troubleshooting

Please refer to Monitor Azure Functions for troubleshooting guidance.

Next steps

Read the introduction to Azure Functions or creating an Azure Function guide.

Contributing

See our CONTRIBUTING.md for details on building, testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Microsoft.Azure.WebJobs.Extensions.EventHubs:

Package Downloads
MassTransit.WebJobs.EventHubs

MassTransit Azure WebJobs Event Hubs support; MassTransit is a message-based distributed application framework for .NET http://masstransit-project.com

RedQuick

Package Description

Microsoft.Azure.Workflows.WebJobs.Extension

Extensions for running workflows in Azure Functions

Beef.Events.EventHubs

Business Entity Execution Framework (Beef) Event Hubs framework.

Amido.Stacks.Messaging.Azure.EventHub

Package Description

GitHub repositories (11)

Showing the top 5 popular GitHub repositories that depend on Microsoft.Azure.WebJobs.Extensions.EventHubs:

Repository Stars
MassTransit/MassTransit
Distributed Application Framework for .NET
Azure/azure-webjobs-sdk
Azure WebJobs SDK
mspnp/cloud-design-patterns
Prescriptive Architecture Guidance for Cloud Applications
JamesRandall/FunctionMonkey
Write more elegant Azure Functions with less boilerplate, more consistency, and support for REST APIs. Docs can be found at https://functionmonkey.azurefromthetrenches.com
ProfessionalCSharp/MoreSamples
Additional code samples the book series Professional C#, Wrox Press
Version Downloads Last updated
5.0.0 9,973 10/26/2021
5.0.0-beta.7 16,788 7/9/2021
5.0.0-beta.6 10,884 6/9/2021
5.0.0-beta.5 10,250 5/18/2021
5.0.0-beta.4 12,696 4/6/2021
5.0.0-beta.3 12,710 3/11/2021
5.0.0-beta.2 13,299 3/9/2021
5.0.0-beta.1 16,486 2/10/2021
4.3.0 5,521 10/26/2021
4.2.0 427,813 12/11/2020
4.1.1 906,352 2/5/2020
4.1.0 160,698 10/31/2019
4.0.1 122,536 10/1/2019
4.0.0 136,141 9/19/2019
4.0.0-beta2 767 8/5/2019
4.0.0-beta1 4,351 5/14/2019
3.0.6 256,783 7/23/2019
3.0.5 150,167 5/3/2019
3.0.4 50,275 3/29/2019
3.0.3 196,766 3/7/2019
3.0.2 43,657 1/25/2019
3.0.1 107,424 10/17/2018
3.0.0 112,977 9/19/2018
3.0.0-rc1 2,636 9/14/2018
3.0.0-beta8 8,134 8/30/2018
3.0.0-beta5 14,318 3/26/2018
3.0.0-beta4 4,703 11/28/2017
3.0.0-beta3 1,184 9/15/2017