|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- # Serialization
-
- We provide the `ISerializer` interface to support serialization of messages. By default, json is used to serialize messages and store them in the database.
-
- ## Custom Serialization
-
- ```C#
- 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
-
- ```
-
- ## Message Adapter (removed in v3.0)
-
- 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**](../storage/general.md#_7). This time maybe you need to customize the message wapper.
-
- CAP provides the `IMessagePacker` interface for customizing the [**Wrapper Object**](../storage/general.md#_7). Custom MessagePacker usually packs and unpacks the `CapMessage` In this process you can add your own business objects.
-
- Usage :
-
- ```csharp
-
- 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.
-
- ```csharp
-
- services.AddCap(x =>{ }).AddMessagePacker<MyMessagePacker>();
-
- ```
|