/// <summary> Publishes a batch of CloudEvents to an Azure Event Grid topic. </summary> /// <param name="events"> An array of events to be published to Event Grid. </param> /// <param name="async">Whether to invoke the operation asynchronously.</param> /// <param name="cancellationToken"> The cancellation token to use. </param> private async Task <Response> SendCloudEventsInternal(IEnumerable <CloudEvent> events, bool async, CancellationToken cancellationToken = default) { using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(EventGridPublisherClient)}.{nameof(SendEvents)}"); scope.Start(); try { // List of events cannot be null Argument.AssertNotNull(events, nameof(events)); List <CloudEventInternal> eventsWithSerializedPayloads = new List <CloudEventInternal>(); foreach (CloudEvent cloudEvent in events) { // Individual events cannot be null Argument.AssertNotNull(cloudEvent, nameof(cloudEvent)); CloudEventInternal newCloudEvent = new CloudEventInternal( cloudEvent.Id, cloudEvent.Source, cloudEvent.Type, "1.0") { Time = cloudEvent.Time, DataBase64 = cloudEvent.DataBase64, Datacontenttype = cloudEvent.DataContentType, Dataschema = cloudEvent.DataSchema, Subject = cloudEvent.Subject }; foreach (KeyValuePair <string, object> kvp in cloudEvent.ExtensionAttributes) { newCloudEvent.Add(kvp.Key, new CustomModelSerializer(kvp.Value, _dataSerializer, cancellationToken)); } // The 'Data' property is optional for CloudEvents // Additionally, if the type of data is binary, 'Data' will not be populated (data will be stored in 'DataBase64' instead) if (cloudEvent.Data != null) { JsonDocument data = SerializeObjectToJsonDocument(cloudEvent.Data, cloudEvent.Data.GetType(), cancellationToken); newCloudEvent.Data = data.RootElement; } eventsWithSerializedPayloads.Add(newCloudEvent); } if (async) { // Publish asynchronously if called via an async path return(await _serviceRestClient.PublishCloudEventEventsAsync( _hostName, eventsWithSerializedPayloads, cancellationToken).ConfigureAwait(false)); } else { return(_serviceRestClient.PublishCloudEventEvents( _hostName, eventsWithSerializedPayloads, cancellationToken)); } } catch (Exception e) { scope.Failed(e); throw; } }
/// <summary> Publishes a batch of CloudEvents to an Azure Event Grid topic. </summary> /// <param name="events"> An array of events to be published to Event Grid. </param> /// <param name="async">Whether to invoke the operation asynchronously</param> /// <param name="cancellationToken"> The cancellation token to use. </param> private async Task <Response> PublishCloudEventsInternal(IEnumerable <CloudEvent> events, bool async, CancellationToken cancellationToken = default) { using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(EventGridPublisherClient)}.{nameof(PublishCloudEvents)}"); scope.Start(); try { // List of events cannot be null Argument.AssertNotNull(events, nameof(events)); List <CloudEventInternal> eventsWithSerializedPayloads = new List <CloudEventInternal>(); foreach (CloudEvent cloudEvent in events) { // Individual events cannot be null Argument.AssertNotNull(cloudEvent, nameof(cloudEvent)); CloudEventInternal newCloudEvent = new CloudEventInternal( cloudEvent.Id, cloudEvent.Source, cloudEvent.Type, cloudEvent.SpecVersion) { Time = cloudEvent.Time, Datacontenttype = cloudEvent.DataContentType, Dataschema = cloudEvent.DataSchema, Subject = cloudEvent.Subject }; foreach (KeyValuePair <string, object> kvp in cloudEvent.ExtensionAttributes) { newCloudEvent.Add(kvp.Key, new EventGridSerializer(kvp.Value, _serializer, cancellationToken)); } // The 'Data' property is optional for CloudEvents if (cloudEvent.Data != null) { if (cloudEvent.Data is IEnumerable <byte> enumerable) { newCloudEvent.DataBase64 = Convert.ToBase64String(enumerable.ToArray()); } else if (cloudEvent.Data is ReadOnlyMemory <byte> memory) { newCloudEvent.DataBase64 = Convert.ToBase64String(memory.ToArray()); } else { newCloudEvent.Data = new EventGridSerializer( cloudEvent.Data, _serializer, cancellationToken); } } eventsWithSerializedPayloads.Add(newCloudEvent); } if (async) { // Publish asynchronously if called via an async path return(await _serviceRestClient.PublishCloudEventEventsAsync( _hostName, eventsWithSerializedPayloads, cancellationToken).ConfigureAwait(false)); } else { return(_serviceRestClient.PublishCloudEventEvents( _hostName, eventsWithSerializedPayloads, cancellationToken)); } } catch (Exception e) { scope.Failed(e); throw; } }