I currently have an EventHub instance set up in Azure. It has 5 partitions. What I want to know if if the PartitionKey always has to be a number between 0 and n-1 with n being the number of partitions.
I have the following code:
private static async Task SendMessagesToEventHub(int numMessagesToSend)
{
var sender = eventHubClient.CreatePartitionSender("test1");
for (var i = 0; i < numMessagesToSend; i++)
{
try
{
var message = $"Message {i}";
Console.WriteLine($"Sending message: {message}");
await sender.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
}
await Task.Delay(10);
}
Console.WriteLine($"{numMessagesToSend} messages sent.");
}
This the throws an exception
The specified partition is invalid for an EventHub partition sender or receiver. It should be between 0 and 4.
In the documentation of EventHub, this is what they say regarding the PartitionKey:
The EventData class has a PartitionKey property that enables the sender to specify a value that is hashed to produce a partition assignment. Using a partition key ensures that all the events with the same key are sent to the same partition in the Event Hub. Common partition keys include user session IDs and unique sender IDs.
To me this means that you are not limited to an int but can use any string. What am I missing?