77
88using System ;
99using System . ComponentModel ;
10- using System . Reflection ;
1110using Datadog . Trace . ClrProfiler . CallTarget ;
1211using Datadog . Trace . Configuration ;
13- using Datadog . Trace . DuckTyping ;
1412using Datadog . Trace . Logging ;
1513
1614namespace Datadog . Trace . ClrProfiler . AutoInstrumentation . MassTransit . FilterInjection ;
@@ -93,79 +91,43 @@ private static void RegisterDatadogConfigureReceiveEndpoint(object collection)
9391
9492 var collectionType = collection . GetType ( ) ;
9593
96- // Find the MassTransit and DI assemblies
97- Assembly ? massTransitAssembly = null ;
98- Assembly ? diAssembly = null ;
99-
100- foreach ( var assembly in AppDomain . CurrentDomain . GetAssemblies ( ) )
101- {
102- var assemblyName = assembly . GetName ( ) . Name ;
103- if ( assemblyName == "MassTransit" )
104- {
105- massTransitAssembly = assembly ;
106- }
107- else if ( assemblyName == "Microsoft.Extensions.DependencyInjection.Abstractions" )
108- {
109- diAssembly = assembly ;
110- }
111-
112- if ( massTransitAssembly != null && diAssembly != null )
113- {
114- break ;
115- }
116- }
117-
118- if ( massTransitAssembly == null )
119- {
120- Log . Debug ( "MassTransit AddMassTransitIntegration: Could not find MassTransit assembly" ) ;
121- return ;
122- }
123-
124- var configureReceiveEndpointType = massTransitAssembly . GetType ( "MassTransit.IConfigureReceiveEndpoint" ) ;
94+ // Get IConfigureReceiveEndpoint type from MassTransit assembly
95+ var configureReceiveEndpointType = MassTransitCommon . GetConfigureReceiveEndpointType ( ) ;
12596 if ( configureReceiveEndpointType == null )
12697 {
12798 Log . Debug ( "MassTransit AddMassTransitIntegration: Could not find IConfigureReceiveEndpoint type" ) ;
12899 return ;
129100 }
130101
131102 // Create a reverse duck type instance that implements IConfigureReceiveEndpoint
132- var datadogImpl = new DatadogConfigureReceiveEndpoint ( ) ;
133- var datadogProxy = DuckType . CreateReverse ( configureReceiveEndpointType , datadogImpl ) ;
134-
103+ var datadogProxy = MassTransitCommon . CreateConfigureReceiveEndpointProxy ( new DatadogConfigureReceiveEndpoint ( ) ) ;
135104 if ( datadogProxy == null )
136105 {
137106 Log . Debug ( "MassTransit AddMassTransitIntegration: Could not create reverse duck type for IConfigureReceiveEndpoint" ) ;
138107 return ;
139108 }
140109
141- if ( diAssembly == null )
142- {
143- Log . Debug ( "MassTransit AddMassTransitIntegration: Could not find DI Abstractions assembly" ) ;
144- return ;
145- }
146-
147- // Find ServiceDescriptor type
148- var serviceDescriptorType = diAssembly . GetType ( "Microsoft.Extensions.DependencyInjection.ServiceDescriptor" ) ;
110+ // Find ServiceDescriptor and ServiceLifetime types from DI assembly
111+ var serviceDescriptorType = MassTransitCommon . GetServiceDescriptorType ( ) ;
149112 if ( serviceDescriptorType == null )
150113 {
151114 Log . Debug ( "MassTransit AddMassTransitIntegration: Could not find ServiceDescriptor type" ) ;
152115 return ;
153116 }
154117
155- // Find ServiceLifetime enum
156- var serviceLifetimeType = diAssembly . GetType ( "Microsoft.Extensions.DependencyInjection.ServiceLifetime" ) ;
118+ var serviceLifetimeType = MassTransitCommon . GetServiceLifetimeType ( ) ;
157119 if ( serviceLifetimeType == null )
158120 {
159121 Log . Debug ( "MassTransit AddMassTransitIntegration: Could not find ServiceLifetime type" ) ;
160122 return ;
161123 }
162124
163125 // Create ServiceDescriptor for our implementation (Scoped lifetime)
164- // ServiceDescriptor.Describe(typeof(IConfigureReceiveEndpoint), _ => datadogProxy, ServiceLifetime.Scoped)
165126 var scopedLifetime = Enum . ToObject ( serviceLifetimeType , 1 ) ; // ServiceLifetime.Scoped = 1
166127
167128 // Create a factory func that returns our proxy instance
168- var serviceProviderType = diAssembly . GetType ( "System.IServiceProvider" ) ?? typeof ( IServiceProvider ) ;
129+ var diAssembly = MassTransitCommon . GetDiAbstractionsAssembly ( ) ;
130+ var serviceProviderType = diAssembly ? . GetType ( "System.IServiceProvider" ) ?? typeof ( IServiceProvider ) ;
169131 var funcType = typeof ( Func < , > ) . MakeGenericType ( serviceProviderType , configureReceiveEndpointType ) ;
170132
171133 // Create a delegate that returns our singleton proxy
@@ -182,11 +144,11 @@ private static void RegisterDatadogConfigureReceiveEndpoint(object collection)
182144 var factory = factoryMethod . Invoke ( null , new [ ] { datadogProxy } ) ;
183145
184146 // Find the ServiceDescriptor constructor: ServiceDescriptor(Type serviceType, Func<IServiceProvider, object> factory, ServiceLifetime lifetime)
185- var descriptorCtor = serviceDescriptorType . GetConstructor ( new [ ] { typeof ( Type ) , funcType , serviceLifetimeType } ) ;
147+ var descriptorCtor = serviceDescriptorType . GetConstructor ( [ typeof ( Type ) , funcType , serviceLifetimeType ] ) ;
186148 if ( descriptorCtor == null )
187149 {
188150 // Try the simpler overload
189- descriptorCtor = serviceDescriptorType . GetConstructor ( new [ ] { typeof ( Type ) , typeof ( object ) , serviceLifetimeType } ) ;
151+ descriptorCtor = serviceDescriptorType . GetConstructor ( [ typeof ( Type ) , typeof ( object ) , serviceLifetimeType ] ) ;
190152 }
191153
192154 if ( descriptorCtor == null )
@@ -195,11 +157,11 @@ private static void RegisterDatadogConfigureReceiveEndpoint(object collection)
195157 return ;
196158 }
197159
198- var descriptor = descriptorCtor . Invoke ( new [ ] { configureReceiveEndpointType , factory , scopedLifetime } ) ;
160+ var descriptor = descriptorCtor . Invoke ( [ configureReceiveEndpointType , factory , scopedLifetime ] ) ;
199161
200162 // Add the descriptor to the collection
201163 // collection.Add(descriptor)
202- var addMethod = collectionType . GetMethod ( "Add" , new [ ] { serviceDescriptorType } ) ;
164+ var addMethod = collectionType . GetMethod ( "Add" , [ serviceDescriptorType ] ) ;
203165 if ( addMethod == null )
204166 {
205167 // Try ICollection<ServiceDescriptor>.Add
@@ -209,8 +171,8 @@ private static void RegisterDatadogConfigureReceiveEndpoint(object collection)
209171
210172 if ( addMethod != null )
211173 {
212- addMethod . Invoke ( collection , new [ ] { descriptor } ) ;
213- Log . Information ( "MassTransit AddMassTransitIntegration: Successfully registered DatadogConfigureReceiveEndpoint" ) ;
174+ addMethod . Invoke ( collection , [ descriptor ] ) ;
175+ Log . Debug ( "MassTransit AddMassTransitIntegration: Successfully registered DatadogConfigureReceiveEndpoint" ) ;
214176 }
215177 else
216178 {
0 commit comments