Beispiel #1
0
        public async Task PublishToSpecificPartition()
        {
            await using var scope = await EventHubScope.CreateAsync(1);

            #region Snippet:EventHubs_Migrate_T1_PublishToSpecificPartition
#if SNIPPET
            var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
            var eventHubName     = "<< NAME OF THE EVENT HUB >>";
#else
            var connectionString = TestUtility.EventHubsConnectionString;
            var eventHubName     = scope.EventHubName;
#endif

            var builder = new EventHubsConnectionStringBuilder(connectionString);
            builder.EntityPath = eventHubName;

            EventHubClient  client = EventHubClient.CreateFromConnectionString(builder.ToString());
            PartitionSender sender = default;

            try
            {
                using var eventBatch = client.CreateBatch();

                for (var index = 0; index < 5; ++index)
                {
                    var eventData = new EventData(Encoding.UTF8.GetBytes($"Event #{ index }"));

                    if (!eventBatch.TryAdd(eventData))
                    {
                        throw new Exception($"The event at { index } could not be added");
                    }
                }

                string firstPartition = (await client.GetRuntimeInformationAsync()).PartitionIds.First();
                sender = client.CreatePartitionSender(firstPartition);

                await sender.SendAsync(eventBatch);
            }
            finally
            {
                sender?.Close();
                client.Close();
            }

            #endregion
        }