Skip to content

Commit b0f68ef

Browse files
authored
Add EventHub extension README.md (Azure#17514)
Fixes: Azure#16646
1 parent b526d01 commit b0f68ef

File tree

7 files changed

+216
-8
lines changed

7 files changed

+216
-8
lines changed

eng/Packages.Data.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@
150150
<PackageReference Update="FsCheck.Xunit" Version="2.14.0" />
151151
<PackageReference Update="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
152152
<PackageReference Update="Microsoft.AspNetCore.Server.WebListener" Version="1.0.2" />
153-
<PackageReference Update="Microsoft.Azure.Devices.Client" Version="1.27.0" />
154153
<PackageReference Update="Microsoft.Azure.Devices" Version="1.19.0" />
154+
<PackageReference Update="Microsoft.Azure.Devices.Client" Version="1.27.0" />
155155
<PackageReference Update="Microsoft.Azure.Graph.RBAC" Version="2.2.2-preview" />
156156
<PackageReference Update="Microsoft.Azure.KeyVault.Core" Version="3.0.3" />
157157
<PackageReference Update="Microsoft.Azure.Management.ContainerRegistry" Version="2.0.0" />
@@ -167,6 +167,7 @@
167167
<PackageReference Update="Microsoft.Azure.Storage.Blob" Version="11.1.7" />
168168
<PackageReference Update="Microsoft.Azure.Storage.Queue" Version="11.1.7" />
169169
<PackageReference Update="Microsoft.Azure.Test.HttpRecorder" Version="[1.13.3, 2.0.0)" />
170+
<PackageReference Update="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
170171
<PackageReference Update="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.2" />
171172
<PackageReference Update="Microsoft.CSharp" Version="4.6" />
172173
<PackageReference Update="Microsoft.Extensions.Azure" Version="1.0.0" />

sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/README.md

Lines changed: 122 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,151 @@
11
# Azure WebJobs Event Hubs client library for .NET
22

3-
TODO
3+
This extension provides functionality for accessing Azure Event Hubs from an Azure Function.
44

55
## Getting started
66

77
### Install the package
88

9-
Install the Event Grid extension with [NuGet][nuget]:
9+
Install the Event Hubs extension with [NuGet](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.EventHubs):
1010

1111
```Powershell
1212
dotnet add package Microsoft.Azure.WebJobs.Extensions.EventHubs --version 5.0.0-beta.1
1313
```
1414

1515
### Prerequisites
1616

17-
TODO
17+
- **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](https://azure.microsoft.com/free) or use your [Visual Studio Subscription](https://visualstudio.microsoft.com/subscriptions/) benefits when you [create an account](https://account.windowsazure.com/Home/Index).
18+
19+
- **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](https://docs.microsoft.com/azure/event-hubs/event-hubs-create). 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.
20+
21+
- **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](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json&tabs=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.
22+
23+
[![Deploy button](http://azuredeploy.net/deploybutton.png)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-sdk-for-net%2Fmaster%2Fsdk%2Feventhub%2FAzure.Messaging.EventHubs.Processor%2Fassets%2Fsamples-azure-deploy.json)
1824

1925
### Authenticate the Client
2026

21-
TODO
27+
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](https://docs.microsoft.com/azure/event-hubs/event-hubs-get-connection-string).
28+
29+
The `Connection` property of `EventHubAttribute` and `EventHubTriggerAttribute` is used to specify the configuration property that stores the connection string.
30+
31+
The `AzureWebJobsStorage` connection string is used to preserve the processing checkpoint information.
32+
33+
For the local development use the `local.settings.json` file to store the connection string:
34+
35+
```json
36+
{
37+
"Values": {
38+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
39+
"<connection_name>": "Endpoint=sb://<event_hub_name>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=Jya7Eh76HU92ibsxuk1ITN8CM8Bt76YLKf5ISjU3jZ8="
40+
}
41+
}
42+
```
43+
44+
When deployed use the [application settings](https://docs.microsoft.com/azure/azure-functions/functions-how-to-use-azure-function-app-settings) to set the connection string.
45+
46+
#### Managed identity authentication
47+
48+
If your environment has [managed identity](https://docs.microsoft.com/azure/app-service/overview-managed-identity?tabs=dotnet) enabled you can use it to authenticate the Event Hubs extension.
49+
To use managed identity provide the `<connection_name>__fullyQualifiedNamespace` configuration setting.
50+
51+
```json
52+
{
53+
"Values": {
54+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
55+
"<connection_name>__fullyQualifiedNamespace": "<event_hub_name>.servicebus.windows.net"
56+
}
57+
}
58+
```
59+
60+
Or in the case of deployed app set the same setting in [application settings](https://docs.microsoft.com/azure/azure-functions/functions-how-to-use-azure-function-app-settings):
61+
62+
```
63+
<connection_name>__fullyQualifiedNamespace=<event_hub_name>.servicebus.windows.net
64+
```
2265

2366
## Key concepts
2467

25-
TODO
68+
### Event Hub Trigger
69+
70+
The Event Hub Trigger allows a function to be executed when a message is sent to an Event Hub.
71+
72+
Please follow the [Azure Event Hubs trigger tutorial](https://docs.microsoft.com/azure/azure-functions/functions-bindings-event-hubs-trigger?tabs=csharp) to learn more about Event Hub triggers.
73+
74+
### Event Hub Output Binding
75+
76+
The Event Hub Output Binding allows a function to send Event Hub events.
77+
78+
Please follow the [Azure Event Hubs output binding](https://docs.microsoft.com/azure/azure-functions/functions-bindings-event-hubs-output?tabs=csharp) to learn more about Event Hub bindings.
2679

2780
## Examples
2881

29-
TODO
82+
### Sending individual event
83+
84+
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.
85+
86+
```C# Snippet:BindingToReturnValue
87+
[FunctionName("BindingToReturnValue")]
88+
[return: EventHub("<event_hub_name>", Connection = "<connection_name>")]
89+
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
90+
{
91+
// This value would get stored in EventHub event body.
92+
// The string would be UTF8 encoded
93+
return $"C# Timer trigger function executed at: {DateTime.Now}";
94+
}
95+
```
96+
97+
### Sending multiple events
98+
99+
To send multiple events from a single Azure Function invocation you can apply the `EventHubAttribute` to the `IAsyncCollector<string>` or `IAsyncCollector<EventData>` parameter.
100+
101+
102+
```C# Snippet:BindingToCollector
103+
[FunctionName("BindingToCollector")]
104+
public static async Task Run(
105+
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
106+
[EventHub("<event_hub_name>", Connection = "<connection_name>")] IAsyncCollector<EventData> collector)
107+
{
108+
// IAsyncCollector allows sending multiple events in a single function invocation
109+
await collector.AddAsync(new EventData(new BinaryData($"Event 1 added at: {DateTime.Now}")));
110+
await collector.AddAsync(new EventData(new BinaryData($"Event 2 added at: {DateTime.Now}")));
111+
}
112+
```
113+
114+
### Per-event triggers
115+
116+
To run a function every time an event is sent to Event Hub apply the `EventHubTriggerAttribute` to a `string` or `EventData` parameter.
117+
118+
```C# Snippet:TriggerSingle
119+
[FunctionName("TriggerSingle")]
120+
public static void Run(
121+
[EventHubTrigger("<event_hub_name>", Connection = "<connection_name>")] string eventBodyAsString,
122+
ILogger logger)
123+
{
124+
logger.LogInformation($"C# function triggered to process a message: {eventBodyAsString}");
125+
}
126+
```
127+
128+
### Batch triggers
129+
130+
To run a function for a batch of received events apply the `EventHubTriggerAttribute` to a `string[]` or `EventData[]` parameter.
131+
132+
```C# Snippet:TriggerBatch
133+
[FunctionName("TriggerBatch")]
134+
public static void Run(
135+
[EventHubTrigger("<event_hub_name>", Connection = "<connection_name>")] EventData[] events,
136+
ILogger logger)
137+
{
138+
foreach (var e in events)
139+
{
140+
logger.LogInformation($"C# function triggered to process a message: {e.EventBody}");
141+
logger.LogInformation($"EnqueuedTime={e.EnqueuedTime}");
142+
}
143+
}
144+
```
30145

31146
## Troubleshooting
32147

33-
TODO
148+
Please refer to [Monitor Azure Functions](https://docs.microsoft.com/azure/azure-functions/functions-monitoring) for troubleshooting guidance.
34149

35150
## Next steps
36151

sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/Microsoft.Azure.WebJobs.Extensions.EventHubs.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<PackageReference Include="Microsoft.Azure.Management.Storage" />
1212
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" />
1313
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" VersionOverride="3.0.23" />
14+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" />
1415
<PackageReference Include="Polly" />
1516
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1617
<PackageReference Include="Moq" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Azure.Messaging.EventHubs;
7+
8+
namespace Microsoft.Azure.WebJobs.Extensions.EventHubs.Tests.Samples
9+
{
10+
public static class BindingToCollector
11+
{
12+
#region Snippet:BindingToCollector
13+
[FunctionName("BindingToCollector")]
14+
public static async Task Run(
15+
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
16+
[EventHub("<event_hub_name>", Connection = "<connection_name>")] IAsyncCollector<EventData> collector)
17+
{
18+
// IAsyncCollector allows sending multiple events in a single function invocation
19+
await collector.AddAsync(new EventData(new BinaryData($"Event 1 added at: {DateTime.Now}")));
20+
await collector.AddAsync(new EventData(new BinaryData($"Event 2 added at: {DateTime.Now}")));
21+
}
22+
#endregion
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Microsoft.Azure.WebJobs.Extensions.EventHubs.Tests.Samples
8+
{
9+
public static class BindingToReturnValueFunction
10+
{
11+
#region Snippet:BindingToReturnValue
12+
[FunctionName("BindingToReturnValue")]
13+
[return: EventHub("<event_hub_name>", Connection = "<connection_name>")]
14+
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
15+
{
16+
// This value would get stored in EventHub event body.
17+
// The string would be UTF8 encoded
18+
return $"C# Timer trigger function executed at: {DateTime.Now}";
19+
}
20+
#endregion
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Azure.Messaging.EventHubs;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Microsoft.Azure.WebJobs.Extensions.EventHubs.Tests.Samples
8+
{
9+
public static class TriggerBatch
10+
{
11+
#region Snippet:TriggerBatch
12+
[FunctionName("TriggerBatch")]
13+
public static void Run(
14+
[EventHubTrigger("<event_hub_name>", Connection = "<connection_name>")] EventData[] events,
15+
ILogger logger)
16+
{
17+
foreach (var e in events)
18+
{
19+
logger.LogInformation($"C# function triggered to process a message: {e.EventBody}");
20+
logger.LogInformation($"EnqueuedTime={e.EnqueuedTime}");
21+
}
22+
}
23+
#endregion
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Microsoft.Azure.WebJobs.Extensions.EventHubs.Tests.Samples
7+
{
8+
public static class TriggerSingle
9+
{
10+
#region Snippet:TriggerSingle
11+
[FunctionName("TriggerSingle")]
12+
public static void Run(
13+
[EventHubTrigger("<event_hub_name>", Connection = "<connection_name>")] string eventBodyAsString,
14+
ILogger logger)
15+
{
16+
logger.LogInformation($"C# function triggered to process a message: {eventBodyAsString}");
17+
}
18+
#endregion
19+
}
20+
}

0 commit comments

Comments
 (0)