Skip to content

Commit 31433b9

Browse files
author
Gábor Zavarkó
committed
netstandard 2.0 fix
1 parent 67a85e7 commit 31433b9

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

projects/RabbitMQ.Client/client/impl/RabbitMQActivitySource.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Net;
45
using System.Net.Sockets;
56
using System.Reflection;
7+
using System.Runtime.CompilerServices;
68
using System.Text;
79
using RabbitMQ.Client.Events;
810
using RabbitMQ.Client.Impl;
@@ -110,6 +112,23 @@ internal static Activity Receive(string routingKey, string exchange, ulong deliv
110112
return activity;
111113
}
112114

115+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
116+
private static string GetString(ReadOnlySpan<byte> span)
117+
{
118+
#if NETSTANDARD
119+
unsafe
120+
{
121+
fixed (byte* bytesPtr = span)
122+
{
123+
return Encoding.UTF8.GetString(bytesPtr, span.Length);
124+
}
125+
}
126+
#else
127+
return Encoding.UTF8.GetString(span);
128+
#endif
129+
}
130+
131+
113132
internal static Activity Deliver(BasicDeliverEventArgs deliverEventArgs)
114133
{
115134
if (!s_subscriberSource.HasListeners())
@@ -123,17 +142,17 @@ internal static Activity Deliver(BasicDeliverEventArgs deliverEventArgs)
123142

124143
ActivityContext.TryParse(traceparent, traceState, out ActivityContext parentContext);
125144

126-
string routingKey = UseRoutingKeyAsOperationName ? Encoding.UTF8.GetString(deliverEventArgs.RoutingKey.Span) : null;
145+
string routingKey = UseRoutingKeyAsOperationName ? GetString(deliverEventArgs.RoutingKey.Span) : null;
127146
Activity activity = s_subscriberSource.StartLinkedRabbitMQActivity(
128147
UseRoutingKeyAsOperationName ? $"{routingKey} deliver" : "deliver",
129148
ActivityKind.Consumer, parentContext);
130149

131150
if (activity != null && activity.IsAllDataRequested)
132151
{
133-
string exchange = Encoding.UTF8.GetString(deliverEventArgs.Exchange.Span);
152+
string exchange = GetString(deliverEventArgs.Exchange.Span);
134153
if (routingKey == null)
135154
{
136-
routingKey = Encoding.UTF8.GetString(deliverEventArgs.RoutingKey.Span);
155+
routingKey = GetString(deliverEventArgs.RoutingKey.Span);
137156
}
138157

139158
PopulateMessagingTags("deliver",

0 commit comments

Comments
 (0)