Class MessageExtensions
- Namespace
- Savvyio.Messaging.Cryptography
- Assembly
- Savvyio.Messaging.dll
Extension methods for the IMessage<T> interface.
public static class MessageExtensions
- Inheritance
-
MessageExtensions
Examples
This example shows how to sign a message before handing it to a transport that requires tamper detection.
using System;
using Savvyio;
using Savvyio.Messaging;
using Savvyio.Messaging.Cryptography;
namespace ExampleApp;
using System;
using System.IO;
using System.Text;
using Savvyio;
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 CryptographicMessageExtensionsExample
{
public ISignedMessage<CreateOrderCommand> Sign()
{
var message = new Message<CreateOrderCommand>("msg-42", new Uri("urn:orders"), "orders.created", new CreateOrderCommand("ORD-42"));
return message.Sign(new DemoMarshaller(), options => options.SignatureSecret = new byte[] { 1, 2, 3 });
}
}
public sealed record CreateOrderCommand(string OrderId) : Request;
Methods
Sign<T>(IMessage<T>, IMarshaller, Action<SignedMessageOptions>)
Converts the specified message to an ISignedMessage<T> equivalent using the provided setup configurator.
public static ISignedMessage<T> Sign<T>(this IMessage<T> message, IMarshaller marshaller, Action<SignedMessageOptions> setup) where T : IRequest
Parameters
messageIMessage<T>The IRequest message to cryptographically sign.
marshallerIMarshallerThe IMarshaller that is used when converting the
messageinto an ISignedMessage<T>.setupAction<SignedMessageOptions>The SignedMessageOptions that must be configured.
Returns
- ISignedMessage<T>
An implementation of the ISignedMessage<T> interface having a constraint to the IRequest interface.
Type Parameters
TThe type of the payload constraint to the IRequest interface.
Exceptions
- ArgumentNullException
messagecannot be null -or-marshallercannot be null.- ArgumentException
setupfailed to configure an instance of SignedMessageOptions in a valid state.