EventHubClient.SendAsync Method

Definition

Overloads

SendAsync(EventData)

Send EventData to EventHub. The sent EventData will land on any arbitrarily chosen EventHubs partition.

There are 3 ways to send to EventHubs, each exposed as a method (along with its sendBatch overload):

i. SendAsync(EventData) or SendAsync(IEnumerable<EventData>)

ii. SendAsync(EventData, String) or SendAsync(IEnumerable<EventData>, String)

iii. SendAsync(EventData) or SendAsync(IEnumerable<EventData>)

Use this method to send if:

a) the SendAsync(EventData) operation should be highly available and

b) the data needs to be evenly distributed among all partitions; exception being, when a subset of partitions are unavailable

SendAsync(EventData) sends the EventData to a Service Gateway, which in-turn will forward the EventData to one of the EventHub's partitions. Here's the message forwarding algorithm:

i. Forward the EventDatas to EventHub partitions, by equally distributing the data among all partitions (ex: Round-robin the EventDatas to all EventHub partitions)

ii. If one of the EventHub partitions is unavailable for a moment, the Service Gateway will automatically detect it and forward the message to another available partition - making the send operation highly-available.

SendAsync(EventDataBatch)

Send a batch of EventData in EventDataBatch.

SendAsync(IEnumerable<EventData>)

Send a batch of EventData to EventHub. The sent EventData will land on any arbitrarily chosen EventHub partition. This is the most recommended way to send to EventHub.

There are 3 ways to send to EventHubs, to understand this particular type of send refer to the overload SendAsync(EventData), which is used to send single EventData. Use this overload if you need to send a batch of EventData.

Sending a batch of EventData's is useful in the following cases:

i. Efficient send - sending a batch of EventData maximizes the overall throughput by optimally using the number of sessions created to EventHub's service.

ii. Send multiple EventData's in a Transaction. To acheieve ACID properties, the Gateway Service will forward all EventData's in the batch to a single EventHub partition.

SendAsync(EventData, String)

Sends an 'EventData with a partitionKey to EventHub. All EventData's with a partitionKey are guaranteed to land on the same partition. This send pattern emphasize data correlation over general availability and latency.

There are 3 ways to send to EventHubs, each exposed as a method (along with its batched overload):

i. SendAsync(EventData) or SendAsync(IEnumerable<EventData>)

ii. SendAsync(EventData, String) or SendAsync(IEnumerable<EventData>, String)

iii. SendAsync(EventData) or SendAsync(IEnumerable<EventData>)

Use this type of send if:

a) There is a need for correlation of events based on Sender instance; The sender can generate a UniqueId and set it as partitionKey - which on the received Message can be used for correlation

b) The client wants to take control of distribution of data across partitions.

Multiple PartitionKeys could be mapped to one Partition. EventHubs service uses a proprietary Hash algorithm to map the PartitionKey to a PartitionId. Using this type of send (Sending using a specific partitionKey) could sometimes result in partitions which are not evenly distributed.
SendAsync(IEnumerable<EventData>, String)

Send a 'batch of EventData with the same partitionKey' to EventHub. All EventData's with a partitionKey are guaranteed to land on the same partition. Multiple PartitionKey's will be mapped to one Partition.

There are 3 ways to send to EventHubs, to understand this particular type of send refer to the overload SendAsync(EventData, String), which is the same type of send and is used to send single EventData.

Sending a batch of EventData's is useful in the following cases:

i. Efficient send - sending a batch of EventData maximizes the overall throughput by optimally using the number of sessions created to EventHubs service.

ii. Sending multiple events in One Transaction. This is the reason why all events sent in a batch needs to have same partitionKey (so that they are sent to one partition only).

SendAsync(EventData)

Send EventData to EventHub. The sent EventData will land on any arbitrarily chosen EventHubs partition.

There are 3 ways to send to EventHubs, each exposed as a method (along with its sendBatch overload):

i. SendAsync(EventData) or SendAsync(IEnumerable<EventData>)

ii. SendAsync(EventData, String) or SendAsync(IEnumerable<EventData>, String)

iii. SendAsync(EventData) or SendAsync(IEnumerable<EventData>)

Use this method to send if:

a) the SendAsync(EventData) operation should be highly available and

b) the data needs to be evenly distributed among all partitions; exception being, when a subset of partitions are unavailable

SendAsync(EventData) sends the EventData to a Service Gateway, which in-turn will forward the EventData to one of the EventHub's partitions. Here's the message forwarding algorithm:

i. Forward the EventDatas to EventHub partitions, by equally distributing the data among all partitions (ex: Round-robin the EventDatas to all EventHub partitions)

ii. If one of the EventHub partitions is unavailable for a moment, the Service Gateway will automatically detect it and forward the message to another available partition - making the send operation highly-available.

public System.Threading.Tasks.Task SendAsync (Microsoft.Azure.EventHubs.EventData eventData);
member this.SendAsync : Microsoft.Azure.EventHubs.EventData -> System.Threading.Tasks.Task
Public Function SendAsync (eventData As EventData) As Task

Parameters

eventData
EventData

the EventData to be sent.

Returns

A Task that completes when the send operations is done.

See also

Applies to

SendAsync(EventDataBatch)

Send a batch of EventData in EventDataBatch.

public System.Threading.Tasks.Task SendAsync (Microsoft.Azure.EventHubs.EventDataBatch eventDataBatch);
member this.SendAsync : Microsoft.Azure.EventHubs.EventDataBatch -> System.Threading.Tasks.Task
Public Function SendAsync (eventDataBatch As EventDataBatch) As Task

Parameters

eventDataBatch
EventDataBatch

the batch of events to send to EventHub

Returns

A Task that completes when the send operation is done.

Applies to

SendAsync(IEnumerable<EventData>)

Send a batch of EventData to EventHub. The sent EventData will land on any arbitrarily chosen EventHub partition. This is the most recommended way to send to EventHub.

There are 3 ways to send to EventHubs, to understand this particular type of send refer to the overload SendAsync(EventData), which is used to send single EventData. Use this overload if you need to send a batch of EventData.

Sending a batch of EventData's is useful in the following cases:

i. Efficient send - sending a batch of EventData maximizes the overall throughput by optimally using the number of sessions created to EventHub's service.

ii. Send multiple EventData's in a Transaction. To acheieve ACID properties, the Gateway Service will forward all EventData's in the batch to a single EventHub partition.

public System.Threading.Tasks.Task SendAsync (System.Collections.Generic.IEnumerable<Microsoft.Azure.EventHubs.EventData> eventDatas);
member this.SendAsync : seq<Microsoft.Azure.EventHubs.EventData> -> System.Threading.Tasks.Task
Public Function SendAsync (eventDatas As IEnumerable(Of EventData)) As Task

Parameters

eventDatas
IEnumerable<EventData>

A batch of events to send to EventHub

Returns

A Task that completes when the send operations is done.

Examples

Sample code:

var client = EventHubClient.Create("__connectionString__");
while (true)
{
    var events = new List<EventData>();
    for (int count = 1; count < 11; count++)
    {
        var payload = new PayloadEvent(count);
        byte[] payloadBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload));
        var sendEvent = new EventData(payloadBytes);
        var applicationProperties = new Dictionary<string, string>();
        applicationProperties["from"] = "csharpClient";
        sendEvent.Properties = applicationProperties;
        events.Add(sendEvent);
    }

    await client.SendAsync(events);
    Console.WriteLine("Sent Batch... Size: {0}", events.Count);
}

See also

Applies to

SendAsync(EventData, String)

Sends an 'EventData with a partitionKey to EventHub. All EventData's with a partitionKey are guaranteed to land on the same partition. This send pattern emphasize data correlation over general availability and latency.

There are 3 ways to send to EventHubs, each exposed as a method (along with its batched overload):

i. SendAsync(EventData) or SendAsync(IEnumerable<EventData>)

ii. SendAsync(EventData, String) or SendAsync(IEnumerable<EventData>, String)

iii. SendAsync(EventData) or SendAsync(IEnumerable<EventData>)

Use this type of send if:

a) There is a need for correlation of events based on Sender instance; The sender can generate a UniqueId and set it as partitionKey - which on the received Message can be used for correlation

b) The client wants to take control of distribution of data across partitions.

Multiple PartitionKeys could be mapped to one Partition. EventHubs service uses a proprietary Hash algorithm to map the PartitionKey to a PartitionId. Using this type of send (Sending using a specific partitionKey) could sometimes result in partitions which are not evenly distributed.
public System.Threading.Tasks.Task SendAsync (Microsoft.Azure.EventHubs.EventData eventData, string partitionKey);
member this.SendAsync : Microsoft.Azure.EventHubs.EventData * string -> System.Threading.Tasks.Task
Public Function SendAsync (eventData As EventData, partitionKey As String) As Task

Parameters

eventData
EventData

the EventData to be sent.

partitionKey
String

the partitionKey will be hashed to determine the partitionId to send the EventData to. On the Received message this can be accessed at PartitionKey.

Returns

A Task that completes when the send operation is done.

See also

Applies to

SendAsync(IEnumerable<EventData>, String)

Send a 'batch of EventData with the same partitionKey' to EventHub. All EventData's with a partitionKey are guaranteed to land on the same partition. Multiple PartitionKey's will be mapped to one Partition.

There are 3 ways to send to EventHubs, to understand this particular type of send refer to the overload SendAsync(EventData, String), which is the same type of send and is used to send single EventData.

Sending a batch of EventData's is useful in the following cases:

i. Efficient send - sending a batch of EventData maximizes the overall throughput by optimally using the number of sessions created to EventHubs service.

ii. Sending multiple events in One Transaction. This is the reason why all events sent in a batch needs to have same partitionKey (so that they are sent to one partition only).

public System.Threading.Tasks.Task SendAsync (System.Collections.Generic.IEnumerable<Microsoft.Azure.EventHubs.EventData> eventDatas, string partitionKey);
member this.SendAsync : seq<Microsoft.Azure.EventHubs.EventData> * string -> System.Threading.Tasks.Task
Public Function SendAsync (eventDatas As IEnumerable(Of EventData), partitionKey As String) As Task

Parameters

eventDatas
IEnumerable<EventData>

the batch of events to send to EventHub

partitionKey
String

the partitionKey will be hashed to determine the partitionId to send the EventData to. On the Received message this can be accessed at PartitionKey.

Returns

A Task that completes when the send operation is done.

See also

Applies to