diff --git a/src/DotNetCore.CAP/ICapPublisher.cs b/src/DotNetCore.CAP/ICapPublisher.cs index e6f5ef2..58f4181 100644 --- a/src/DotNetCore.CAP/ICapPublisher.cs +++ b/src/DotNetCore.CAP/ICapPublisher.cs @@ -24,13 +24,20 @@ namespace DotNetCore.CAP /// Asynchronous publish an object message. /// /// the topic name or exchange router key. - /// message body content, that will be serialized of json. + /// message body content, that will be serialized. /// callback subscriber name /// Task PublishAsync(string name, T contentObj, string callbackName = null, CancellationToken cancellationToken = default); - - Task PublishAsync(string name, T contentObj, IDictionary optionHeaders, CancellationToken cancellationToken = default); + /// + /// Asynchronous publish an object message with custom headers + /// + /// content object + /// the topic name or exchange router key. + /// message body content, that will be serialized. + /// message additional headers. + /// + Task PublishAsync(string name, T contentObj, IDictionary headers, CancellationToken cancellationToken = default); /// /// Publish an object message. @@ -39,5 +46,13 @@ namespace DotNetCore.CAP /// message body content, that will be serialized of json. /// callback subscriber name void Publish(string name, T contentObj, string callbackName = null); + + /// + /// Publish an object message. + /// + /// the topic name or exchange router key. + /// message body content, that will be serialized of json. + /// message additional headers. + void Publish(string name, T contentObj, IDictionary headers); } } \ No newline at end of file diff --git a/src/DotNetCore.CAP/Internal/ICapPublisher.Default.cs b/src/DotNetCore.CAP/Internal/ICapPublisher.Default.cs index 2629911..fea0307 100644 --- a/src/DotNetCore.CAP/Internal/ICapPublisher.Default.cs +++ b/src/DotNetCore.CAP/Internal/ICapPublisher.Default.cs @@ -35,32 +35,30 @@ namespace DotNetCore.CAP.Internal public AsyncLocal Transaction { get; } - public async Task PublishAsync(string name, T value, - IDictionary optionHeaders, - CancellationToken cancellationToken = default) + public async Task PublishAsync(string name, T value, IDictionary headers, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } - if (optionHeaders == null) + if (headers == null) { - optionHeaders = new Dictionary(); + headers = new Dictionary(); } var messageId = SnowflakeId.Default().NextId().ToString(); - optionHeaders.Add(Headers.MessageId, messageId); - optionHeaders.Add(Headers.MessageName, name); - optionHeaders.Add(Headers.Type, typeof(T).FullName); - optionHeaders.Add(Headers.SentTime, DateTimeOffset.Now.ToString()); - if (!optionHeaders.ContainsKey(Headers.CorrelationId)) + headers.Add(Headers.MessageId, messageId); + headers.Add(Headers.MessageName, name); + headers.Add(Headers.Type, typeof(T).FullName); + headers.Add(Headers.SentTime, DateTimeOffset.Now.ToString()); + if (!headers.ContainsKey(Headers.CorrelationId)) { - optionHeaders.Add(Headers.CorrelationId, messageId); - optionHeaders.Add(Headers.CorrelationSequence, 0.ToString()); + headers.Add(Headers.CorrelationId, messageId); + headers.Add(Headers.CorrelationSequence, 0.ToString()); } - var message = new Message(optionHeaders, value); + var message = new Message(headers, value); long? tracingTimestamp = null; try @@ -99,11 +97,6 @@ namespace DotNetCore.CAP.Internal } } - public void Publish(string name, T value, string callbackName = null) - { - PublishAsync(name, value, callbackName).GetAwaiter().GetResult(); - } - public Task PublishAsync(string name, T value, string callbackName = null, CancellationToken cancellationToken = default) { @@ -115,6 +108,16 @@ namespace DotNetCore.CAP.Internal return PublishAsync(name, value, header, cancellationToken); } + public void Publish(string name, T value, string callbackName = null) + { + PublishAsync(name, value, callbackName).GetAwaiter().GetResult(); + } + + public void Publish(string name, T value, IDictionary headers) + { + PublishAsync(name, value, headers).GetAwaiter().GetResult(); + } + #region tracing private long? TracingBefore(Message message) diff --git a/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs b/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs index f50125a..f21a4f0 100644 --- a/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs +++ b/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs @@ -84,6 +84,11 @@ namespace DotNetCore.CAP.Test { throw new NotImplementedException(); } + + public void Publish(string name, T contentObj, IDictionary headers) + { + throw new NotImplementedException(); + } } } } \ No newline at end of file