Commit cb86c952 authored by Savorboard's avatar Savorboard

Support null value of message body.

parent 971c9db9
......@@ -2,7 +2,6 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Confluent.Kafka;
......
......@@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
namespace DotNetCore.CAP
{
......@@ -24,35 +25,35 @@ namespace DotNetCore.CAP
/// Asynchronous publish an object message.
/// </summary>
/// <param name="name">the topic name or exchange router key.</param>
/// <param name="contentObj">message body content, that will be serialized.</param>
/// <param name="contentObj">message body content, that will be serialized. (can be null)</param>
/// <param name="callbackName">callback subscriber name</param>
/// <param name="cancellationToken"></param>
Task PublishAsync<T>(string name, T contentObj, string callbackName = null, CancellationToken cancellationToken = default);
Task PublishAsync<T>(string name, [CanBeNull] T contentObj, string callbackName = null, CancellationToken cancellationToken = default);
/// <summary>
/// Asynchronous publish an object message with custom headers
/// </summary>
/// <typeparam name="T">content object</typeparam>
/// <param name="name">the topic name or exchange router key.</param>
/// <param name="contentObj">message body content, that will be serialized.</param>
/// <param name="contentObj">message body content, that will be serialized. (can be null)</param>
/// <param name="headers">message additional headers.</param>
/// <param name="cancellationToken"></param>
Task PublishAsync<T>(string name, T contentObj, IDictionary<string, string> headers, CancellationToken cancellationToken = default);
Task PublishAsync<T>(string name, [CanBeNull] T contentObj, IDictionary<string, string> headers, CancellationToken cancellationToken = default);
/// <summary>
/// Publish an object message.
/// </summary>
/// <param name="name">the topic name or exchange router key.</param>
/// <param name="contentObj">message body content, that will be serialized of json.</param>
/// <param name="contentObj">message body content, that will be serialized. (can be null)</param>
/// <param name="callbackName">callback subscriber name</param>
void Publish<T>(string name, T contentObj, string callbackName = null);
void Publish<T>(string name, [CanBeNull] T contentObj, string callbackName = null);
/// <summary>
/// Publish an object message.
/// </summary>
/// <param name="name">the topic name or exchange router key.</param>
/// <param name="contentObj">message body content, that will be serialized of json.</param>
/// <param name="contentObj">message body content, that will be serialized. (can be null)</param>
/// <param name="headers">message additional headers.</param>
void Publish<T>(string name, T contentObj, IDictionary<string, string> headers);
void Publish<T>(string name, [CanBeNull] T contentObj, IDictionary<string, string> headers);
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
namespace DotNetCore.CAP.Messages
{
public class Message
{
public Message(IDictionary<string, string> headers, object value)
public Message(IDictionary<string, string> headers, [CanBeNull] object value)
{
Headers = headers ?? throw new ArgumentNullException(nameof(headers));
Value = value ?? throw new ArgumentNullException(nameof(value));
Value = value;
}
public IDictionary<string, string> Headers { get; }
[CanBeNull]
public object Value { get; }
}
......
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
namespace DotNetCore.CAP.Messages
{
......@@ -8,10 +9,10 @@ namespace DotNetCore.CAP.Messages
/// </summary>
public class TransportMessage
{
public TransportMessage(IDictionary<string, string> headers, byte[] body)
public TransportMessage(IDictionary<string, string> headers, [CanBeNull] byte[] body)
{
Headers = headers ?? throw new ArgumentNullException(nameof(headers));
Body = body ?? throw new ArgumentNullException(nameof(body));
Body = body;
}
/// <summary>
......@@ -22,6 +23,7 @@ namespace DotNetCore.CAP.Messages
/// <summary>
/// Gets the body object of this message
/// </summary>
[CanBeNull]
public byte[] Body { get; }
public string GetId()
......
......@@ -13,13 +13,18 @@ namespace DotNetCore.CAP.Serialization
{
public Task<TransportMessage> SerializeAsync(Message message)
{
if (message.Value == null)
{
return Task.FromResult(new TransportMessage(message.Headers, null));
}
var json = JsonConvert.SerializeObject(message.Value);
return Task.FromResult(new TransportMessage(message.Headers, Encoding.UTF8.GetBytes(json)));
}
public Task<Message> DeserializeAsync(TransportMessage transportMessage, Type valueType)
{
if (valueType == null)
if (valueType == null || transportMessage.Body == null)
{
return Task.FromResult(new Message(transportMessage.Headers, null));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment