/// <summary> /// Creates a new handler for handling the messages of the specified subscription /// </summary> /// <param name="subscriptionId">The id of the subscription to handle the messages for</param> /// <returns>A new handler for handling the messages of the specified subscription</returns> protected virtual EventHandler <StanMsgHandlerArgs> CreateEventHandlerFor(string subscriptionId) { return(async(sender, e) => { this.Logger.LogInformation("An event with subject '{subject}' has been received from the underlying NATS Streaming sink.", e.Message.Subject); this.Logger.LogInformation("Dispatching the resulting cloud event with subject '{subject}' to the gateway...", e.Message.Subject); CloudEvent cloudEvent = CloudEventBuilder.FromEvent(this.EventFormatter.DecodeJObject(JsonConvert.DeserializeObject <JObject>(Encoding.UTF8.GetString(e.Message.Data)))) .WithSubscriptionId(subscriptionId) .WithSequence(e.Message.Sequence) .HasBeenRedelivered(e.Message.Redelivered) .Build(); CloudEventContent cloudEventContent = new CloudEventContent(cloudEvent, ContentMode.Structured, new JsonEventFormatter()); using (HttpResponseMessage response = await this.HttpClient.PostAsync("pub", cloudEventContent)) { string content = await response.Content?.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { this.Logger.LogError($"An error occured while dispatching a cloud event of type '{{eventType}}' to the eventing controller: the remote server response with a '{{statusCode}}' status code.{Environment.NewLine}Details: {{responseContent}}", cloudEvent.Type, response.StatusCode, content); response.EnsureSuccessStatusCode(); } } this.Logger.LogInformation("The cloud event with subject '{subject}' has been successfully dispatched to the gateway.", e.Message.Subject); e.Message.Ack(); }); }
/// <summary> /// Dispatches the <see cref="ResolvedEvent"/> to the <see cref="CloudEvent"/> gateway /// </summary> /// <param name="subscriptionId">The id of the subscription the <see cref="CloudEvent"/> originates from</param> /// <param name="e">The <see cref="ResolvedEvent"/> to dispatch</param> /// <returns>A new awaitable <see cref="Task"/></returns> protected virtual async Task DispatchEventAsync(string subscriptionId, ResolvedEvent e) { try { this.Logger.LogInformation("An event has been received from the underlying EventStore sink."); this.Logger.LogInformation("Dispatching the resulting cloud event to the gateway..."); CloudEvent cloudEvent = CloudEventBuilder.FromEvent(this.EventFormatter.DecodeJObject(JsonConvert.DeserializeObject <JObject>(Encoding.UTF8.GetString(e.Event.Data)))) .WithSubscriptionId(subscriptionId) .WithSequence((ulong)e.Event.EventNumber) .Build(); CloudEventContent cloudEventContent = new CloudEventContent(cloudEvent, ContentMode.Structured, new JsonEventFormatter()); using (HttpResponseMessage response = await this.HttpClient.PostAsync("pub", cloudEventContent)) { string content = await response.Content?.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { this.Logger.LogError($"An error occured while dispatching a cloud event of type '{{eventType}}' to the eventing controller: the remote server response with a '{{statusCode}}' status code.{Environment.NewLine}Details: {{responseContent}}", cloudEvent.Type, response.StatusCode, content); response.EnsureSuccessStatusCode(); } } this.Logger.LogInformation("The cloud event has been successfully dispatched to the gateway."); } catch (HttpRequestException) { throw; } catch (Exception ex) { this.Logger.LogError($"An error occured while dispatching a cloud event to the gateway.{Environment.NewLine}Details: {{ex}}", ex.Message); throw; } }
public void GivenSimpleDataContractEvent_WhenBuild_ThenCloudEventIsBuiltWithDefaultValues() { // Given var p = new Faker().Person; var userCreated = new UserCreatedDataContract { BirthDate = p.DateOfBirth, FirstName = p.FirstName, LastName = p.LastName, Id = p.Random.Guid() }; // When var builder = new CloudEventBuilder <UserCreatedDataContract>(); var actual = builder .WithData(userCreated) .Build(); // Then actual.Should().NotBeNull(); actual.Data.Should().Be(userCreated); }
public void GivenASingleEventSerialized_WhenDeserialize_ThenResultIsEquivalentToSource() { // Given var p = new Faker().Person; var userCreatedDataContract = new UserCreatedRecordDataContract { BirthDate = p.DateOfBirth, FirstName = p.FirstName, LastName = p.LastName, Id = p.Random.Guid() }; var expected = new CloudEventBuilder <UserCreatedRecordDataContract>().WithData(userCreatedDataContract) .Build(); var formatter = new JsonEventFormatter <UserCreatedRecordDataContract>(); var bytes = formatter.EncodeStructuredModeMessage(expected, out var ct); // When var e = formatter.DecodeStructuredModeMessage(bytes, ct, new List <CloudEventAttribute>()); CloudEvent <UserCreatedRecordDataContract> actual = e; // Then actual.Should().BeEquivalentTo(expected); }