We provide the ISerializer
interface to support serialization of messages. By default, json is used to serialize messages and store them in the database.
public class YourSerializer: ISerializer
{
Task<TransportMessage> SerializeAsync(Message message)
{
}
Task<Message> DeserializeAsync(TransportMessage transportMessage, Type valueType)
{
}
}
Then register your implemented serializer in the container:
services.AddSingleton<ISerializer, YourSerializer>();
// ---
services.AddCap
In heterogeneous systems, sometimes you need to communicate with other systems, but other systems use message objects that may be different from CAP’s Wrapper Object. This time maybe you need to customize the message wapper.
CAP provides the IMessagePacker
interface for customizing the Wrapper Object. Custom MessagePacker usually packs and unpacks the CapMessage
In this process you can add your own business objects.
Usage :
class MyMessagePacker : IMessagePacker
{
private readonly IContentSerializer _serializer;
public DefaultMessagePacker(IContentSerializer serializer)
{
_serializer = serializer;
}
public string Pack(CapMessage obj)
{
var myStructure = new
{
Id = obj.Id,
Body = obj.Content,
Date = obj.Timestamp,
Callback = obj.CallbackName
};
return _serializer.Serialize(myStructure);
}
public CapMessage UnPack(string packingMessage)
{
var myStructure = _serializer.DeSerialize<MyStructure>(packingMessage);
return new CapMessageDto
{
Id = myStructure.Id,
Timestamp = myStructure.Date,
Content = myStructure.Body,
CallbackName = myStructure.Callback
};
}
}
Next, add the custom MyMessagePacker
to the service.
services.AddCap(x =>{ }).AddMessagePacker<MyMessagePacker>();