1717
1818namespace MQTTLib
1919{
20- public class MqttClient
20+ public class MqttClient
2121 {
2222 static Dictionary < Guid , MqttClient > s_instances = new Dictionary < Guid , MqttClient > ( ) ;
2323 static Dictionary < string , string > s_topicProcs = new Dictionary < string , string > ( ) ;
@@ -111,7 +111,7 @@ public static MqttStatus Connect(string url, MqttConfig config)
111111 Certificates = new List < X509Certificate > ( ) { caCert , cliCert } ,
112112 //CertificateValidationHandler = context => true
113113 CertificateValidationCallback = ( certificate , chain , sslError , opts ) => true
114-
114+
115115 } ;
116116
117117 b = b . WithTls ( tls ) ;
@@ -228,30 +228,28 @@ public static MqttStatus Subscribe(Guid key, string topic, string gxproc, int qo
228228 UserProperties = new List < MqttUserProperty > { new MqttUserProperty ( "gxrocedure" , $ "{{\" gxrocedure\" :\" { gxproc } \" }}") }
229229 } ;
230230
231- MqttClientSubscribeResult result = mqtt . m_mqttClient . SubscribeAsync ( options ) . GetAwaiter ( ) . GetResult ( ) ;
232- s_topicProcs [ GetProcKey ( mqtt , topic ) ] = gxproc ;
233-
234231 mqtt . m_mqttClient . UseApplicationMessageReceivedHandler ( msg =>
235232 {
236233 if ( msg == null || msg . ApplicationMessage == null || msg . ApplicationMessage . Payload == null )
237234 return ;
238235
239236 Console . WriteLine ( $ "Message arrived! Topic:{ msg . ApplicationMessage . Topic } Payload:{ Encoding . UTF8 . GetString ( msg . ApplicationMessage . Payload ) } ") ;
240237
241- Assembly asm = Assembly . LoadFrom ( fullPath ) ;
242- Type procType = asm . GetTypes ( ) . FirstOrDefault ( t => t . FullName . EndsWith ( gxproc , StringComparison . InvariantCultureIgnoreCase ) ) ;
238+ try
239+ {
240+ Assembly asm = Assembly . LoadFrom ( fullPath ) ;
241+ Type procType = asm . GetTypes ( ) . FirstOrDefault ( t => t . FullName . EndsWith ( gxproc , StringComparison . InvariantCultureIgnoreCase ) ) ;
243242
244- if ( procType == null )
245- throw new InvalidDataException ( "Data type not found" ) ;
243+ if ( procType == null )
244+ throw new InvalidDataException ( "Data type not found" ) ;
246245
247- var methodInfo = procType . GetMethod ( "execute" , new Type [ ] { typeof ( string ) , typeof ( string ) } ) ;
248- if ( methodInfo == null )
249- throw new NotImplementedException ( "Method 'execute' not found" ) ;
246+ var methodInfo = procType . GetMethod ( "execute" , new Type [ ] { typeof ( string ) , typeof ( string ) } ) ;
247+ if ( methodInfo == null )
248+ throw new NotImplementedException ( "Method 'execute' not found" ) ;
249+
250+ var obj = Activator . CreateInstance ( procType ) ;
250251
251- var obj = Activator . CreateInstance ( procType ) ;
252252
253- try
254- {
255253 methodInfo . Invoke ( obj , new object [ ] { msg . ApplicationMessage . Topic , Encoding . UTF8 . GetString ( msg . ApplicationMessage . Payload ) } ) ;
256254 }
257255 catch ( Exception ex )
@@ -267,6 +265,10 @@ public static MqttStatus Subscribe(Guid key, string topic, string gxproc, int qo
267265 }
268266
269267 } ) ;
268+
269+ MqttClientSubscribeResult result = mqtt . m_mqttClient . SubscribeAsync ( options ) . GetAwaiter ( ) . GetResult ( ) ;
270+ s_topicProcs [ GetProcKey ( mqtt , topic ) ] = gxproc ;
271+
270272 }
271273 catch ( Exception ex )
272274 {
0 commit comments