minor-refactoring-for-demos (#59)
* Showcasing EH client usage with async in mind. - replacing FromResult<object>(null) with Task.CompletedTask - removing .Wait() with proper invocation from console app * moving using statement under namespace * removing configureAwait from sample * removing configureAwait from sample closing connection moving client outside of the SendMessagesToEventHub method * updating corresponding samples
This commit is contained in:
Родитель
1ea2d39dab
Коммит
e943b7a9b1
|
@ -4,6 +4,7 @@
|
|||
namespace SampleEphReceiver
|
||||
{
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Azure.EventHubs;
|
||||
using Microsoft.Azure.EventHubs.Processor;
|
||||
|
||||
|
@ -18,6 +19,11 @@ namespace SampleEphReceiver
|
|||
private static readonly string StorageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
MainAsync(args).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private static async Task MainAsync(string[] args)
|
||||
{
|
||||
Console.WriteLine("Registering EventProcessor...");
|
||||
|
||||
|
@ -29,13 +35,13 @@ namespace SampleEphReceiver
|
|||
StorageContainerName);
|
||||
|
||||
// Registers the Event Processor Host and starts receiving messages
|
||||
eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>().Wait();
|
||||
await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();
|
||||
|
||||
Console.WriteLine("Receiving. Press enter key to stop worker.");
|
||||
Console.ReadLine();
|
||||
|
||||
// Disposes of the Event Processor Host
|
||||
eventProcessorHost.UnregisterEventProcessorAsync().Wait();
|
||||
await eventProcessorHost.UnregisterEventProcessorAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,19 +15,19 @@ namespace SampleEphReceiver
|
|||
public Task CloseAsync(PartitionContext context, CloseReason reason)
|
||||
{
|
||||
Console.WriteLine($"Processor Shutting Down. Partition '{context.PartitionId}', Reason: '{reason}'.");
|
||||
return Task.FromResult<object>(null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task OpenAsync(PartitionContext context)
|
||||
{
|
||||
Console.WriteLine($"SimpleEventProcessor initialized. Partition: '{context.PartitionId}'");
|
||||
return Task.FromResult<object>(null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task ProcessErrorAsync(PartitionContext context, Exception error)
|
||||
{
|
||||
Console.WriteLine($"Error on Partition: {context.PartitionId}, Error: {error.Message}");
|
||||
return Task.FromResult<object>(null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Get started receiving messages with the EventProcessorHost in .NET Core
|
||||
# Get started receiving messages with the EventProcessorHost in .NET Core
|
||||
|
||||
## What will be accomplished
|
||||
|
||||
|
@ -36,13 +36,13 @@ In this tutorial, we will write a .NET Core console application to receive messa
|
|||
|
||||
```cs
|
||||
using Microsoft.Azure.EventHubs;
|
||||
using Microsoft.Azure.EventHubs.Processor;
|
||||
using Microsoft.Azure.EventHubs.Processor;
|
||||
```
|
||||
|
||||
3. Implement the `IEventProcessor` interface. The class should look like this:
|
||||
|
||||
```cs
|
||||
namespace SampleReceiver
|
||||
namespace SampleEphReceiver
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -50,36 +50,36 @@ In this tutorial, we will write a .NET Core console application to receive messa
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.Azure.EventHubs;
|
||||
using Microsoft.Azure.EventHubs.Processor;
|
||||
|
||||
|
||||
public class SimpleEventProcessor : IEventProcessor
|
||||
{
|
||||
public Task CloseAsync(PartitionContext context, CloseReason reason)
|
||||
{
|
||||
Console.WriteLine($"Processor Shutting Down. Partition '{context.PartitionId}', Reason: '{reason}'.");
|
||||
return Task.FromResult<object>(null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
public Task OpenAsync(PartitionContext context)
|
||||
{
|
||||
Console.WriteLine($"SimpleEventProcessor initialized. Partition: '{context.PartitionId}'");
|
||||
return Task.FromResult<object>(null);
|
||||
Console.WriteLine($"SimpleEventProcessor initialized. Partition: '{context.PartitionId}'");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
public Task ProcessErrorAsync(PartitionContext context, Exception error)
|
||||
{
|
||||
Console.WriteLine($"Error on Partition: {context.PartitionId}, Error: {error.Message}");
|
||||
return Task.FromResult<object>(null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
|
||||
|
||||
public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
|
||||
{
|
||||
foreach (var eventData in messages)
|
||||
{
|
||||
var data = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
|
||||
Console.WriteLine($"Message received. Partition: '{context.PartitionId}', Data: '{data}'");
|
||||
Console.WriteLine($"Message received. Partition: '{context.PartitionId}', Data: '{data}'");
|
||||
}
|
||||
|
||||
await context.CheckpointAsync();
|
||||
|
||||
return context.CheckpointAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -106,26 +106,34 @@ In this tutorial, we will write a .NET Core console application to receive messa
|
|||
private static readonly string StorageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);
|
||||
```
|
||||
|
||||
3. Add the following code to the `Main` method:
|
||||
3. Add a new method named `MainAsync` to the `Program` class like the following:
|
||||
```cs
|
||||
private static async Task MainAsync(string[] args)
|
||||
{
|
||||
Console.WriteLine("Registering EventProcessor...");
|
||||
|
||||
var eventProcessorHost = new EventProcessorHost(
|
||||
EhEntityPath,
|
||||
PartitionReceiver.DefaultConsumerGroupName,
|
||||
EhConnectionString,
|
||||
StorageConnectionString,
|
||||
StorageContainerName);
|
||||
|
||||
// Registers the Event Processor Host and starts receiving messages
|
||||
await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();
|
||||
|
||||
Console.WriteLine("Receiving. Press enter key to stop worker.");
|
||||
Console.ReadLine();
|
||||
|
||||
// Disposes of the Event Processor Host
|
||||
await eventProcessorHost.UnregisterEventProcessorAsync();
|
||||
}
|
||||
```
|
||||
|
||||
3. Add the following line of code to the `Main` method:
|
||||
|
||||
```cs
|
||||
Console.WriteLine("Registering EventProcessor...");
|
||||
|
||||
var eventProcessorHost = new EventProcessorHost(
|
||||
EhEntityPath,
|
||||
PartitionReceiver.DefaultConsumerGroupName,
|
||||
EhConnectionString,
|
||||
StorageConnectionString,
|
||||
StorageContainerName);
|
||||
|
||||
// Registers the Event Processor Host and starts receiving messages
|
||||
eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>().Wait();
|
||||
|
||||
Console.WriteLine("Receiving. Press enter key to stop worker.");
|
||||
Console.ReadLine();
|
||||
|
||||
// Disposes of the Event Processor Host
|
||||
eventProcessorHost.UnregisterEventProcessorAsync().Wait();
|
||||
MainAsync(args).GetAwaiter().GetResult();
|
||||
```
|
||||
|
||||
Here is what your Program.cs file should look like:
|
||||
|
@ -134,9 +142,10 @@ In this tutorial, we will write a .NET Core console application to receive messa
|
|||
namespace SampleEphReceiver
|
||||
{
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Azure.EventHubs;
|
||||
using Microsoft.Azure.EventHubs.Processor;
|
||||
|
||||
|
||||
public class Program
|
||||
{
|
||||
private const string EhConnectionString = "{Event Hubs connection string}";
|
||||
|
@ -144,28 +153,33 @@ In this tutorial, we will write a .NET Core console application to receive messa
|
|||
private const string StorageContainerName = "{Storage account container name}";
|
||||
private const string StorageAccountName = "{Storage account name}";
|
||||
private const string StorageAccountKey = "{Storage account key}";
|
||||
|
||||
|
||||
private static readonly string StorageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);
|
||||
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
MainAsync(args).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private static async Task MainAsync(string[] args)
|
||||
{
|
||||
Console.WriteLine("Registering EventProcessor...");
|
||||
|
||||
|
||||
var eventProcessorHost = new EventProcessorHost(
|
||||
EhEntityPath,
|
||||
EhEntityPath,
|
||||
PartitionReceiver.DefaultConsumerGroupName,
|
||||
EhConnectionString,
|
||||
StorageConnectionString,
|
||||
StorageContainerName);
|
||||
|
||||
|
||||
// Registers the Event Processor Host and starts receiving messages
|
||||
eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>().Wait();
|
||||
|
||||
await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();
|
||||
|
||||
Console.WriteLine("Receiving. Press enter key to stop worker.");
|
||||
Console.ReadLine();
|
||||
|
||||
|
||||
// Disposes of the Event Processor Host
|
||||
eventProcessorHost.UnregisterEventProcessorAsync().Wait();
|
||||
await eventProcessorHost.UnregisterEventProcessorAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,18 +10,16 @@ namespace SampleSender
|
|||
|
||||
public class Program
|
||||
{
|
||||
private static EventHubClient eventHubClient;
|
||||
private const string EhConnectionString = "{Event Hubs connection string}";
|
||||
private const string EhEntityPath = "{Event Hub path/name}";
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
SendMessagesToEventHub(100).Wait();
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
MainAsync(args).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
// Creates an Event Hub client and sends 100 messages to the event hub.
|
||||
private static async Task SendMessagesToEventHub(int numMessagesToSend)
|
||||
private static async Task MainAsync(string[] args)
|
||||
{
|
||||
// Creates an EventHubsConnectionStringBuilder object from a the connection string, and sets the EntityPath.
|
||||
// Typically the connection string should have the Entity Path in it, but for the sake of this simple scenario
|
||||
|
@ -31,8 +29,19 @@ namespace SampleSender
|
|||
EntityPath = EhEntityPath
|
||||
};
|
||||
|
||||
var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
|
||||
eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
|
||||
|
||||
await SendMessagesToEventHub(100);
|
||||
|
||||
await eventHubClient.CloseAsync();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
// Creates an Event Hub client and sends 100 messages to the event hub.
|
||||
private static async Task SendMessagesToEventHub(int numMessagesToSend)
|
||||
{
|
||||
for (var i = 0; i < numMessagesToSend; i++)
|
||||
{
|
||||
try
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Get started sending messages to Event Hubs in .NET Core
|
||||
# Get started sending messages to Event Hubs in .NET Core
|
||||
|
||||
## What will be accomplished
|
||||
|
||||
|
@ -41,14 +41,15 @@ To send messages to an Event Hub, we will write a C# console application using V
|
|||
2. Add constants to the `Program` class for the Event Hubs connection string and entity path (individual Event Hub name). Replace the placeholders in brackets with the proper values that were obtained when creating the Event Hub.
|
||||
|
||||
```cs
|
||||
private static EventHubClient eventHubClient;
|
||||
private const string EhConnectionString = "{Event Hubs connection string}";
|
||||
private const string EhEntityPath = "{Event Hub path/name}";
|
||||
```
|
||||
|
||||
3. Add a new method to the `Program` class like the following:
|
||||
3. Add a new method named `MainAsync` to the `Program` class like the following:
|
||||
|
||||
```cs
|
||||
private static async Task SendMessagesToEventHub(int numMessagesToSend)
|
||||
private static async Task MainAsync(string[] args)
|
||||
{
|
||||
// Creates an EventHubsConnectionStringBuilder object from a the connection string, and sets the EntityPath.
|
||||
// Typically the connection string should have the Entity Path in it, but for the sake of this simple scenario
|
||||
|
@ -58,8 +59,23 @@ To send messages to an Event Hub, we will write a C# console application using V
|
|||
EntityPath = EhEntityPath
|
||||
};
|
||||
|
||||
var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
|
||||
eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
|
||||
|
||||
await SendMessagesToEventHub(100);
|
||||
|
||||
await eventHubClient.CloseAsync();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
```
|
||||
|
||||
4. Add a new method named `SendMessagesToEventHub` to the `Program` class like the following:
|
||||
|
||||
```cs
|
||||
// Creates an Event Hub client and sends 100 messages to the event hub.
|
||||
private static async Task SendMessagesToEventHub(int numMessagesToSend)
|
||||
{
|
||||
for (var i = 0; i < numMessagesToSend; i++)
|
||||
{
|
||||
try
|
||||
|
@ -80,12 +96,10 @@ To send messages to an Event Hub, we will write a C# console application using V
|
|||
}
|
||||
```
|
||||
|
||||
4. Add the following code to the `Main` method in the `Program` class.
|
||||
5. Add the following code to the `Main` method in the `Program` class.
|
||||
|
||||
```cs
|
||||
SendMessagesToEventHub(100).Wait();
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
MainAsync(args).GetAwaiter().GetResult();
|
||||
```
|
||||
|
||||
Here is what your Program.cs should look like.
|
||||
|
@ -97,21 +111,19 @@ To send messages to an Event Hub, we will write a C# console application using V
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Azure.EventHubs;
|
||||
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static EventHubClient eventHubClient;
|
||||
private const string EhConnectionString = "{Event Hubs connection string}";
|
||||
private const string EhEntityPath = "{Event Hub path/name}";
|
||||
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
SendMessagesToEventHub(100).Wait();
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
MainAsync(args).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
// Creates an Event Hub client and sends 100 messages to the event hub.
|
||||
private static async Task SendMessagesToEventHub(int numMessagesToSend)
|
||||
|
||||
private static async Task MainAsync(string[] args)
|
||||
{
|
||||
// Creates an EventHubsConnectionStringBuilder object from a the connection string, and sets the EntityPath.
|
||||
// Typically the connection string should have the Entity Path in it, but for the sake of this simple scenario
|
||||
|
@ -120,9 +132,20 @@ To send messages to an Event Hub, we will write a C# console application using V
|
|||
{
|
||||
EntityPath = EhEntityPath
|
||||
};
|
||||
|
||||
var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
|
||||
|
||||
|
||||
eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
|
||||
|
||||
await SendMessagesToEventHub(100);
|
||||
|
||||
await eventHubClient.CloseAsync();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
// Creates an Event Hub client and sends 100 messages to the event hub.
|
||||
private static async Task SendMessagesToEventHub(int numMessagesToSend)
|
||||
{
|
||||
for (var i = 0; i < numMessagesToSend; i++)
|
||||
{
|
||||
try
|
||||
|
@ -135,16 +158,16 @@ To send messages to an Event Hub, we will write a C# console application using V
|
|||
{
|
||||
Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
|
||||
}
|
||||
|
||||
|
||||
await Task.Delay(10);
|
||||
}
|
||||
|
||||
|
||||
Console.WriteLine($"{numMessagesToSend} messages sent.");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
5. Run the program, and ensure that there are no errors thrown.
|
||||
6. Run the program, and ensure that there are no errors thrown.
|
||||
|
||||
Congratulations! You have now created an Event Hub and sent messages to it.
|
||||
Congratulations! You have now sent messages to an Event Hub.
|
||||
|
|
Загрузка…
Ссылка в новой задаче