@@ -25,6 +25,8 @@ internal sealed class RabbitMqClient : IRabbitMqClient
2525 private readonly string _spanContextHeader ;
2626 private readonly bool _persistMessages ;
2727 private readonly int _maxChannels ;
28+ private readonly string _contentType ;
29+ private readonly string _contentEncoding ;
2830
2931 private readonly ConcurrentDictionary < int , IChannel > _channels = new ( ) ;
3032
@@ -48,6 +50,8 @@ public RabbitMqClient(
4850 _spanContextHeader = options . GetSpanContextHeader ( ) ;
4951 _persistMessages = options ? . MessagesPersisted ?? false ;
5052 _maxChannels = options . MaxProducerChannels <= 0 ? 1000 : options . MaxProducerChannels ;
53+ _contentType = options . Publish ? . ContentType ?? "application/json" ;
54+ _contentEncoding = options . Publish ? . ContentEncoding ?? "UTF-8" ;
5155 }
5256
5357 public async Task SendAsync (
@@ -109,8 +113,8 @@ public async Task SendAsync(
109113 var properties = new BasicProperties
110114 {
111115 AppId = _appOptions . Service ,
112- ContentEncoding = _serializer . ContentEncoding ,
113- ContentType = _serializer . ContentType ,
116+ ContentEncoding = _contentEncoding ,
117+ ContentType = _contentType ,
114118 Persistent = _persistMessages ,
115119 MessageId = string . IsNullOrWhiteSpace ( messageId ) ? Guid . NewGuid ( ) . ToString ( "N" ) : messageId ,
116120 CorrelationId = string . IsNullOrWhiteSpace ( correlationId ) ? Guid . NewGuid ( ) . ToString ( "N" ) : correlationId ,
@@ -121,7 +125,7 @@ public async Task SendAsync(
121125
122126 if ( _contextEnabled )
123127 {
124- IncludeMessageContext ( messageContext , properties ) ;
128+ await IncludeMessageContextAsync ( messageContext , properties , cancellationToken ) ;
125129 }
126130
127131 if ( ! string . IsNullOrWhiteSpace ( spanContext ) )
@@ -152,7 +156,7 @@ public async Task SendAsync(
152156 properties . CorrelationId ) ;
153157 }
154158
155- var body = _serializer . Serialize ( message ) ;
159+ var body = await _serializer . SerializeAsync ( message , _contentType , cancellationToken ) ;
156160
157161 await channel . BasicPublishAsync (
158162 exchange : convention . Exchange ,
@@ -163,7 +167,7 @@ await channel.BasicPublishAsync(
163167 cancellationToken : cancellationToken ) ;
164168 }
165169
166- private void IncludeMessageContext ( object context , BasicProperties properties )
170+ private async Task IncludeMessageContextAsync ( object context , BasicProperties properties , CancellationToken cancellationToken )
167171 {
168172 if ( properties ? . Headers is null )
169173 {
@@ -172,7 +176,7 @@ private void IncludeMessageContext(object context, BasicProperties properties)
172176
173177 if ( context is not null )
174178 {
175- properties . Headers . Add ( _contextProvider . HeaderName , _serializer . Serialize ( context ) ) ;
179+ properties . Headers . Add ( _contextProvider . HeaderName , await _serializer . SerializeAsync ( context , _contentType , cancellationToken ) ) ;
176180
177181 return ;
178182 }
0 commit comments