/// <summary> /// Performs the tasks needed to initialize and set up the environment for the test scenario. /// When multiple instances are run in parallel, the setup will take place once, prior to the /// execution of the first test instance. /// </summary> /// public async override Task GlobalSetupAsync() { await base.GlobalSetupAsync().ConfigureAwait(false); s_scope = await EventHubScope.CreateAsync(4).ConfigureAwait(false); s_producer = new EventHubProducerClient(TestEnvironment.EventHubsConnectionString, s_scope.EventHubName); s_sendOptions = await CreateSendOptions(s_producer).ConfigureAwait(false); s_eventBody = EventGenerator.CreateRandomBody(Options.Size); // Publish an empty event to force the connection and link to be established. await s_producer.SendAsync(new[] { new EventData(Array.Empty <byte>()) }, s_sendOptions).ConfigureAwait(false); }
public LineCounterService( IAzureClientFactory <EventHubProducerClient> producerFactory, BlobServiceClient blobServiceClient, ILogger <LineCounterService> logger, IConfiguration configuration) { _logger = logger; _blobContainerClient = blobServiceClient.GetBlobContainerClient("uploads"); _resultsClient = producerFactory.CreateClient("Results"); _processor = new EventProcessorClient( _blobContainerClient, EventHubConsumerClient.DefaultConsumerGroupName, configuration["Uploads:connectionString"], configuration["Uploads:eventHubName"]); }
public EventHubFeed( EventHubProducerClient client, ILoggerFactory log, IHttp http, IJson json, ISettings settings) { this.client = client; this.log = log.CreateLogger <EventHubFeed>(); this.http = http; this.json = json; this.endpoint = settings.GetSetting("DATABRICKS_HOST"); this.token = settings.GetSetting("DATABRICKS_TOKEN"); this.deleteUserJobId = long.Parse(settings.GetSetting("DATABRICKS_DELETE_USER_JOB_ID")); }
public static async Task SendMessageAsync( EventHubProducerClient eventHubProducerClient, string message) { var eventDataBatch = await eventHubProducerClient.CreateBatchAsync(); var eventData = new EventData( Encoding.UTF8.GetBytes(message)); eventDataBatch.TryAdd(eventData); await eventHubProducerClient.SendAsync(eventDataBatch); }
private static async Task CreateSenderAndReceiver(string connectionString) { Console.Write("Creating the Sender and Receivers... "); var partitionId = await GetPartitionId(connectionString); var producerOptions = new EventHubProducerClientOptions { PartitionId = partitionId, }; sender = new EventHubProducerClient(connectionString, producerOptions); receiver = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, partitionId, EventPosition.Latest, connectionString); Console.WriteLine("\tdone"); }
static async Task Main() { await using (var producerClient = new EventHubProducerClient(ConnectionString, EventHubName)) { using (EventDataBatch eventBatch = await producerClient.CreateBatchAsync()) { eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Hello world one"))); eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Hello world two"))); await producerClient.SendAsync(eventBatch); Console.WriteLine("Events are published."); } } }
public async Task CloseSurfacesExceptionsForTransportConsumers() { var mockTransportProducer = new Mock<TransportProducer>(); var mockConnection = new MockConnection(() => mockTransportProducer.Object); var mockBatch = new EventDataBatch(new MockTransportBatch(), new SendOptions { PartitionId = "1" });; var producer = new EventHubProducerClient(mockConnection); mockTransportProducer .Setup(producer => producer.CloseAsync(It.IsAny<CancellationToken>())) .Returns(Task.FromException(new InvalidCastException())); try { await producer.SendAsync(mockBatch); } catch {} Assert.That(() => producer.Close(), Throws.InstanceOf<Exception>()); }
public void ConnectionStringConstructorCreatesDefaultOptions() { var expected = new EventHubProducerClientOptions().RetryOptions; var connectionString = "Endpoint=sb://somehost.com;SharedAccessKeyName=ABC;SharedAccessKey=123;EntityPath=somehub"; var producer = new EventHubProducerClient(connectionString); var policy = GetRetryPolicy(producer); Assert.That(policy, Is.Not.Null, "There should have been a retry policy set."); Assert.That(policy, Is.InstanceOf <BasicRetryPolicy>(), "The default retry policy should be a basic policy."); var actual = ((BasicRetryPolicy)policy).Options; Assert.That(actual.IsEquivalentTo(expected), Is.True, "The default retry policy should be based on the default retry options."); }
public void ExpandedConstructorCreatesDefaultOptions() { var credential = new Mock <EventHubTokenCredential>(Mock.Of <TokenCredential>(), "{namespace}.servicebus.windows.net"); var expected = new EventHubProducerClientOptions().RetryOptions; var producer = new EventHubProducerClient("namespace", "eventHub", credential.Object); var policy = GetRetryPolicy(producer); Assert.That(policy, Is.Not.Null, "There should have been a retry policy set."); Assert.That(policy, Is.InstanceOf <BasicRetryPolicy>(), "The default retry policy should be a basic policy."); var actual = ((BasicRetryPolicy)policy).Options; Assert.That(actual.IsEquivalentTo(expected), Is.True, "The default retry policy should be based on the default retry options."); }
public async Task CloseClosesTheTransportProducers() { var transportProducer = new ObservableTransportProducerMock(); var mockFirstBatch = new EventDataBatch(new MockTransportBatch(), new SendOptions { PartitionId = "1" }); var mockSecondBatch = new EventDataBatch(new MockTransportBatch(), new SendOptions { PartitionId = "2" }); var producer = new EventHubProducerClient(new MockConnection(() => transportProducer)); try { await producer.SendAsync(mockFirstBatch); } catch {} try { await producer.SendAsync(mockSecondBatch); } catch {} producer.Close(); Assert.That(transportProducer.WasCloseCalled, Is.True); Assert.That(transportProducer.CloseCallCount, Is.EqualTo(3)); }
public void ConnectionConstructorCreatesDefaultOptions() { var expected = new EventHubProducerClientOptions().RetryOptions; var mockConnection = new MockConnection(); var producer = new EventHubProducerClient(mockConnection); var policy = GetRetryPolicy(producer); Assert.That(policy, Is.Not.Null, "There should have been a retry policy set."); Assert.That(policy, Is.InstanceOf <BasicRetryPolicy>(), "The default retry policy should be a basic policy."); var actual = ((BasicRetryPolicy)policy).Options; Assert.That(actual.IsEquivalentTo(expected), Is.True, "The default retry policy should be based on the default retry options."); }
private const string eventHubName = "kdbdatawithcaptureoption"; //"EventHubName"; public static async Task SendToEventHub(string eventData) { await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) { // Create a batch of events using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); // Add events to the batch. An event is a represented by a collection of bytes and metadata. eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(eventData))); // Use the producer client to send the batch of events to the event hub producerClient.SendAsync(eventBatch).Wait(); Console.WriteLine("A batch of events has been published."); } }
public async Task CreateBatchInvokesTheTransportProducer() { var batchOptions = new CreateBatchOptions { PartitionKey = "Hi", MaximumSizeInBytes = 9999 }; var transportProducer = new ObservableTransportProducerMock(); var producer = new EventHubProducerClient(new MockConnection(() => transportProducer)); await producer.CreateBatchAsync(batchOptions); Assert.That(transportProducer.CreateBatchCalledWith, Is.Not.Null, "The batch creation should have passed options."); Assert.That(transportProducer.CreateBatchCalledWith, Is.Not.SameAs(batchOptions), "The options should have been cloned."); Assert.That(transportProducer.CreateBatchCalledWith.PartitionKey, Is.EqualTo(batchOptions.PartitionKey), "The partition key should match."); Assert.That(transportProducer.CreateBatchCalledWith.MaximumSizeInBytes, Is.EqualTo(batchOptions.MaximumSizeInBytes), "The maximum size should match."); }
public async Task GetPartitionPropertiesUsesTheRetryPolicy() { var mockConnection = new MockConnection(); var retryPolicy = Mock.Of <EventHubsRetryPolicy>(); var options = new EventHubProducerClientOptions { RetryOptions = new RetryOptions { CustomRetryPolicy = retryPolicy } }; var producer = new EventHubProducerClient(mockConnection, options); await producer.GetPartitionPropertiesAsync("1"); Assert.That(mockConnection.GetPartitionPropertiesInvokedWith, Is.SameAs(retryPolicy), "Either the call was not delegated or the retry policy was not passed."); }
public async Task EventHub_InitialOffsetFromEnqueuedTime() { await using var producer = new EventHubProducerClient(EventHubsTestEnvironment.Instance.EventHubsConnectionString, _eventHubScope.EventHubName); for (int i = 0; i < 3; i++) { // send one at a time so they will have slightly different enqueued times await producer.SendAsync(new EventData[] { new EventData(new BinaryData("data")) }); await Task.Delay(1000); } await using var consumer = new EventHubConsumerClient( EventHubConsumerClient.DefaultConsumerGroupName, EventHubsTestEnvironment.Instance.EventHubsConnectionString, _eventHubScope.EventHubName); var events = consumer.ReadEventsAsync(); _initialOffsetEnqueuedTimeUTC = DateTime.UtcNow; await foreach (PartitionEvent evt in events) { // use the timestamp from the first event for our FromEnqueuedTime _initialOffsetEnqueuedTimeUTC = evt.Data.EnqueuedTime; break; } var initialOffsetOptions = new InitialOffsetOptions(); var(jobHost, host) = BuildHost <EventHubTestInitialOffsetFromEnqueuedTimeJobs>( builder => { builder.ConfigureServices(services => { services.Configure <EventHubOptions>(options => { options.InitialOffsetOptions.Type = OffsetType.FromEnqueuedTime; // Reads from enqueue time are non-inclusive. To ensure that we start with the desired event, set the time slightly in the past. var dto = DateTimeOffset.Parse(_initialOffsetEnqueuedTimeUTC.AddMilliseconds(-150).ToString("yyyy-MM-ddTHH:mm:ssZ")); options.InitialOffsetOptions.EnqueuedTimeUtc = dto; }); }); ConfigureTestEventHub(builder); }); using (jobHost) { bool result = _eventWait.WaitOne(Timeout); Assert.True(result); } }
public async Task Sample09_AccessingEventData() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:Sample09_AccessingEventData #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubProducerClient(connectionString, eventHubName); try { using var eventBatch = await producer.CreateBatchAsync(); var observableBatch = new ObservableEventDataBatch(eventBatch); // Attempt to add events to the batch. for (var index = 0; index < 5; ++index) { var eventData = new EventData($"Event #{ index }"); if (!observableBatch.TryAdd(eventData)) { throw new Exception($"The event at { index } could not be added."); } } // Events in the batch can be inspected using the "Events" collection. foreach (var singleEvent in observableBatch.Events) { Debug.WriteLine($"Added event { singleEvent.EventBody } at time { singleEvent.EnqueuedTime }"); } await producer.SendAsync(observableBatch); } finally { await producer.CloseAsync(); } #endregion }
public async Task ConnectionStringParse() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample06_ConnectionStringParse var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; /*@@*/ /*@@*/ connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; /*@@*/ eventHubName = scope.EventHubName; EventHubsConnectionStringProperties properties = EventHubsConnectionStringProperties.Parse(connectionString); TokenCredential credential = new DefaultAzureCredential(); /*@@*/ /*@@*/ credential = EventHubsTestEnvironment.Instance.Credential; var producer = new EventHubProducerClient( properties.FullyQualifiedNamespace, properties.EventHubName ?? eventHubName, credential); try { using var eventBatch = await producer.CreateBatchAsync(); for (var index = 0; index < 5; ++index) { var eventBody = new BinaryData($"Event #{ index }"); var eventData = new EventData(eventBody); if (!eventBatch.TryAdd(eventData)) { throw new Exception($"The event at { index } could not be added."); } } await producer.SendAsync(eventBatch); } finally { await producer.CloseAsync(); } #endregion }
public async Task EventHubProducerCreatesDiagnosticScopeOnBatchSend() { using var testListener = new ClientDiagnosticListener(DiagnosticSourceName); var eventHubName = "SomeName"; var endpoint = new Uri("amqp://endpoint"); var fakeConnection = new MockConnection(endpoint, eventHubName); var eventCount = 0; var batchTransportMock = new Mock <TransportEventBatch>(); batchTransportMock .Setup(m => m.TryAdd(It.IsAny <EventData>())) .Returns(() => { eventCount++; return(eventCount <= 3); }); var transportMock = new Mock <TransportProducer>(); transportMock .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendEventOptions>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); transportMock .Setup(m => m.CreateBatchAsync(It.IsAny <CreateBatchOptions>(), It.IsAny <CancellationToken>())) .Returns(new ValueTask <TransportEventBatch>(Task.FromResult(batchTransportMock.Object))); var producer = new EventHubProducerClient(fakeConnection, transportMock.Object); var eventData = new EventData(ReadOnlyMemory <byte> .Empty); EventDataBatch batch = await producer.CreateBatchAsync(); Assert.True(batch.TryAdd(eventData)); await producer.SendAsync(batch); ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName, new KeyValuePair <string, string>(DiagnosticProperty.TypeAttribute, DiagnosticProperty.EventHubProducerType), new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext), new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName), new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint.ToString())); ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName); Assert.That(eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match."); Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance."); }
/// <summary> /// Construct a sink that saves log events to the specified EventHubClient. /// </summary> /// <param name="eventHubClient">The EventHubClient to use in this sink.</param> /// <param name="formatter">Provides formatting for outputting log data</param> /// <param name="batchSizeLimit"></param> /// <param name="period"></param> public AzureEventHubBatchingSink( EventHubProducerClient eventHubClient, ITextFormatter formatter, int batchSizeLimit, TimeSpan period) : base(batchSizeLimit, period) { if (batchSizeLimit < 1 || batchSizeLimit > 100) { throw new ArgumentException( "batchSizeLimit must be between 1 and 100."); } _eventHubClient = eventHubClient; _formatter = formatter; }
public async Task ProducerCanOptIntoIdempotentPublishing() { await using (EventHubScope scope = await EventHubScope.CreateAsync(1)) { var connectionString = EventHubsTestEnvironment.Instance.BuildConnectionStringForEventHub(scope.EventHubName); var options = new EventHubProducerClientOptions { EnableIdempotentPartitions = true }; var cancellationSource = new CancellationTokenSource(); cancellationSource.CancelAfter(EventHubsTestEnvironment.Instance.TestExecutionTimeLimit); await using var producer = new EventHubProducerClient(connectionString, options); Assert.That(async() => await producer.GetPartitionIdsAsync(cancellationSource.Token), Throws.Nothing); } }
public void CreateWithConnectionString() { #region Snippet:EventHubs_Migrate_CreateWithConnectionString var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; var consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; /*@@*/ /*@@*/ connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; /*@@*/ eventHubName = "fake"; var producer = new EventHubProducerClient(connectionString, eventHubName); var consumer = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName); #endregion }
public async Task ProducerCanPublishBatchesAfterAnException() { await using (EventHubScope scope = await EventHubScope.CreateAsync(1)) { var cancellationSource = new CancellationTokenSource(); cancellationSource.CancelAfter(EventHubsTestEnvironment.Instance.TestExecutionTimeLimit); var connectionString = EventHubsTestEnvironment.Instance.BuildConnectionStringForEventHub(scope.EventHubName); var options = new EventHubProducerClientOptions { EnableIdempotentPartitions = true }; await using var producer = new EventHubProducerClient(connectionString, options); var partition = (await producer.GetPartitionIdsAsync()).First(); var batchOptions = new CreateBatchOptions { PartitionId = partition }; // Publish a batch to validate that the initial publish works. using var firstBatch = await producer.CreateBatchAsync(batchOptions, cancellationSource.Token); firstBatch.TryAdd(EventGenerator.CreateEvents(1).First()); Assert.That(async() => await producer.SendAsync(firstBatch, cancellationSource.Token), Throws.Nothing, "The first publishing operation was not successful."); // Publish an event too large to succeed; this will force the producer to deal with an exception, which should // update idempotent state. var producerId = (await producer.GetPartitionPublishingPropertiesAsync(partition, cancellationSource.Token)).ProducerGroupId; using var badBatch = EventHubsModelFactory.EventDataBatch(firstBatch.MaximumSizeInBytes + 1000, new List <EventData>(new[] { new EventData(EventGenerator.CreateRandomBody(firstBatch.MaximumSizeInBytes + 1000)) }), new CreateBatchOptions { PartitionId = partition }); Assert.That(async() => await producer.SendAsync(badBatch, cancellationSource.Token), Throws.InstanceOf <EventHubsException>(), "The attempt to publish a too-large event should fail."); // Publish a second batch of events; this will prove that the producer recovered from the exception. using var secondBatch = await producer.CreateBatchAsync(batchOptions, cancellationSource.Token); secondBatch.TryAdd(EventGenerator.CreateEvents(1).First()); secondBatch.TryAdd(EventGenerator.CreateEvents(1).First()); Assert.That(async() => await producer.SendAsync(secondBatch, cancellationSource.Token), Throws.Nothing, "The second publishing operation was not successful."); var newProducerId = (await producer.GetPartitionPublishingPropertiesAsync(partition, cancellationSource.Token)).ProducerGroupId; Assert.That(newProducerId, Is.Not.Null, "The producer group identifier should have a value."); Assert.That(newProducerId, Is.Not.EqualTo(producerId), "The producer group identifier should have been updated after the exception."); } }
private async Task Produce(EventHubProducerClient producer, CancellationToken cancellationToken) { var id = 0; while (!cancellationToken.IsCancellationRequested) { var batchEvents = new List <EventData>(); Console.WriteLine("await producer.CreateBatchAsync()"); using var batch = await producer.CreateBatchAsync(); for (var i = 0; i < Options.PublishBatchSize; i++) { var currentId = id++; Console.WriteLine($"Published: {currentId}"); _published.TryAdd(currentId, 0); var e = new EventData(BitConverter.GetBytes(currentId)); if (!batch.TryAdd(e)) { break; } batchEvents.Add(e); } try { Console.WriteLine("await producer.SendAsync()"); await producer.SendAsync(batch, cancellationToken); Interlocked.Add(ref Metrics.EventsPublished, batch.Count); Interlocked.Increment(ref Metrics.TotalServiceOperations); } catch { // SendAsync failed, so remove events from tracking foreach (var batchEvent in batchEvents) { var currentId = BitConverter.ToInt32(batchEvent.Body.ToArray(), startIndex: 0); Console.WriteLine($"Removed: {currentId}"); _published.TryRemove(currentId, out _); } } } }
static async Task Main(string[] args) { string[] Commands = System.Environment.GetCommandLineArgs(); // Configration initialize Config config = new Config(); // Read configration value from ./config.json config.ReadConfigFile(); if (Commands.Length < 2) { DisplayAbstract(); return; } else { // Read configration values from ./sender.config.json config.ReadCmdArgs(Commands); } if (config.connectionString != "") { Console.WriteLine("-----------------"); config.PrintValue(); Console.WriteLine("-----------------"); await using (var producerClient = new EventHubProducerClient(config.connectionString, config.eventHubName)) { // Create a batch of events using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); // Add events to the batch. An event is a represented by a collection of bytes and metadata. Console.WriteLine("A batch of events started to be published."); for (int i = 0; i < config.count; i++) { String eventmsg = config.msgprefix + " event " + (i + 1) + "/" + config.count + " " + DateTime.Now.ToString(); Console.WriteLine(eventmsg); eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(eventmsg))); } // Use the producer client to send the batch of events to the event hub await producerClient.SendAsync(eventBatch); Console.WriteLine("A batch of events has been published."); } } }
public async Task PartitionId() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample04_PartitionId #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubProducerClient(connectionString, eventHubName); try { string firstPartition = (await producer.GetPartitionIdsAsync()).First(); var batchOptions = new CreateBatchOptions { PartitionId = firstPartition }; using var eventBatch = await producer.CreateBatchAsync(batchOptions); for (var index = 0; index < 5; ++index) { var eventBody = new BinaryData($"Event #{ index }"); var eventData = new EventData(eventBody); if (!eventBatch.TryAdd(eventData)) { throw new Exception($"The event at { index } could not be added."); } } await producer.SendAsync(eventBatch); } finally { await producer.CloseAsync(); } #endregion }
public async Task CustomMetadata() { #region Snippet:EventHubs_Sample04_CustomMetadata var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; /*@@*/ /*@@*/ connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; /*@@*/ eventHubName = _scope.EventHubName; var producer = new EventHubProducerClient(connectionString, eventHubName); try { using var eventBatch = await producer.CreateBatchAsync(); var eventBody = new BinaryData("Hello, Event Hubs!"); var eventData = new EventData(eventBody); eventData.Properties.Add("EventType", "com.microsoft.samples.hello-event"); eventData.Properties.Add("priority", 1); eventData.Properties.Add("score", 9.0); if (!eventBatch.TryAdd(eventData)) { throw new Exception("The first event could not be added."); } eventBody = new BinaryData("Goodbye, Event Hubs!"); eventData = new EventData(eventBody); eventData.Properties.Add("EventType", "com.microsoft.samples.goodbye-event"); eventData.Properties.Add("priority", "17"); eventData.Properties.Add("blob", true); if (!eventBatch.TryAdd(eventData)) { throw new Exception("The second event could not be added."); } await producer.SendAsync(eventBatch); } finally { await producer.CloseAsync(); } #endregion }
private void ConfigureClient() { var connOptions = new EventHubConnectionOptions { TransportType = EventHubsTransportType.AmqpWebSockets }; var co = new EventHubProducerClientOptions { ConnectionOptions = connOptions, RetryOptions = new EventHubsRetryOptions { Mode = EventHubsRetryMode.Exponential } }; _client = new EventHubProducerClient(ConnectionString, EventHubName, co); }
public async Task Inspect() { #region Snippet:EventHubs_ReadMe_Inspect var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; /*@@*/ /*@@*/ connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; /*@@*/ eventHubName = _scope.EventHubName; await using (var producer = new EventHubProducerClient(connectionString, eventHubName)) { string[] partitionIds = await producer.GetPartitionIdsAsync(); } #endregion }
protected async void btnSend_Click(object sender, EventArgs e) { await using (EventHubProducerClient producerClient = new EventHubProducerClient(txtNamespace.Text, txtEventHub.Text, new DefaultAzureCredential())) { // create a batch using (EventDataBatch eventBatch = await producerClient.CreateBatchAsync()) { // add events to the batch. only one in this case. eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(txtData.Text))); // send the batch to the event hub await producerClient.SendAsync(eventBatch); } txtOutput.Text = $"{DateTime.Now} - SENT{Environment.NewLine}{txtOutput.Text}"; } }
/// <summary> /// Runs the sample using the specified Event Hubs connection information. /// </summary> /// /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should target.</param> /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should run against.</param> /// public async Task RunAsync(string connectionString, string eventHubName) { // We will start by creating a producer client, using its default options. await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) { // Because an event consists mainly of an opaque set of bytes, it may be difficult for consumers of those events to // make informed decisions about how to process them. // // In order to allow event publishers to offer better context for consumers, event data may also contain custom metadata, // in the form of a set of key/value pairs. This metadata is not used by, or in any way meaningful to, the Event Hubs // service; it exists only for coordination between event publishers and consumers. // // One common scenario for the inclusion of metadata is to provide a hint about the type of data contained by an event, // so that consumers understand its format and can deserialize it appropriately. // // We will publish a small batch of events based on simple sentences, but will attach some custom metadata with // pretend type names and other hints. Note that the set of metadata is unique to an event; there is no need for every // event in a batch to have the same metadata properties available nor the same data type for those properties. var firstEvent = new EventData(Encoding.UTF8.GetBytes("Hello, Event Hubs!")); firstEvent.Properties.Add("EventType", "com.microsoft.samples.hello-event"); firstEvent.Properties.Add("priority", 1); firstEvent.Properties.Add("score", 9.0); var secondEvent = new EventData(Encoding.UTF8.GetBytes("Goodbye, Event Hubs!")); secondEvent.Properties.Add("EventType", "com.microsoft.samples.goodbye-event"); secondEvent.Properties.Add("priority", "17"); secondEvent.Properties.Add("blob", true); using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); eventBatch.TryAdd(firstEvent); eventBatch.TryAdd(secondEvent); await producerClient.SendAsync(eventBatch); Console.WriteLine("The event batch has been published."); } // At this point, our client has passed its "using" scope and has safely been disposed of. We // have no further obligations. Console.WriteLine(); }