Table of Contents

Class CloudEventExtensions

Namespace
Savvyio.EventDriven.Messaging.CloudEvents.Cryptography
Assembly
Savvyio.EventDriven.Messaging.dll

Extension methods for the ICloudEvent<T> interface.

public static class CloudEventExtensions
Inheritance
CloudEventExtensions

Examples

This example shows how to sign a CloudEvent before handing it to an external bus. The sample includes a simple marshaller and verifies that the resulting signed envelope carries a non-empty signature that subscribers can later validate.

using System;
using System.IO;
using System.Text;
using Savvyio;
using Savvyio.EventDriven;
using Savvyio.EventDriven.Messaging.CloudEvents;
using Savvyio.EventDriven.Messaging.CloudEvents.Cryptography;
using Savvyio.Messaging;
using Savvyio.Messaging.Cryptography;

namespace ExampleApp;

public sealed class DemoMarshaller : IMarshaller
{
    public Stream Serialize<TValue>(TValue value) => new MemoryStream(Encoding.UTF8.GetBytes(value?.ToString() ?? string.Empty));
    public Stream Serialize(object value, Type inputType) => Serialize(value?.ToString() ?? string.Empty);
    public TValue Deserialize<TValue>(Stream data) => throw new NotSupportedException();
    public object Deserialize(Stream data, Type returnType) => throw new NotSupportedException();
}

public sealed class CloudEventCryptographyExtensionsExample
{
    public bool Sign()
    {
        var message = new Message<MemberCreatedEvent>("msg-42", new Uri("https://api.example.com/members"), "members.created", new MemberCreatedEvent("MEM-42"));
        var signed = message.ToCloudEvent().SignCloudEvent(new DemoMarshaller(), options => options.SignatureSecret = new byte[] { 1, 2, 3 });
        return !string.IsNullOrWhiteSpace(signed.Signature);
    }
}

public sealed record MemberCreatedEvent(string MemberId) : Request, IIntegrationEvent;

Methods

SignCloudEvent<T>(ICloudEvent<T>, IMarshaller, Action<SignedMessageOptions>)

Converts the specified cloudEvent to an ISignedCloudEvent<T> equivalent.

public static ISignedCloudEvent<T> SignCloudEvent<T>(this ICloudEvent<T> cloudEvent, IMarshaller marshaller, Action<SignedMessageOptions> setup = null) where T : IIntegrationEvent

Parameters

cloudEvent ICloudEvent<T>

The payload to attach within the message.

marshaller IMarshaller

The IMarshaller that is used when converting the cloudEvent into an ISignedCloudEvent<T>.

setup Action<SignedMessageOptions>

The SignedMessageOptions which may be configured.

Returns

ISignedCloudEvent<T>

An instance of SignedCloudEvent<T> constraint to the IIntegrationEvent interface.

Type Parameters

T

The type of the payload constraint to the IIntegrationEvent interface.

Exceptions

ArgumentNullException

cloudEvent cannot be null -or- marshaller cannot be null.

ArgumentException

setup failed to configure an instance of SignedMessageOptions in a valid state.