mardi 16 août 2022

.Net Emit - Add Try Catch block to existing method in runtime

I use the PubSub mechanism the send events in my application, each event has a custom handler that use to handle it.

My problem is that in many places the handler suppled as the following:

SubscriberService.Subscribe(new SubscribeRequest<string>
{  
   Topic = TopicName,
   Action = async pubSubEvent => await DoSomthingAsync()
}

the return type of Action is void so behind the scenes this lambda translated to async void.

So when an exception occur I have no chance to catch it.

I wondered to myself if there is a way to create a method at runtime that warp the method body of the action with a try-catch block that catches the exception when they occur and write them to the log.

I saw some articles about the Emit.IlGenerator class could help but I have no idea where to start.

I would love to hear any suggestions to deal with these issues.

Thanks!





Aucun commentaire:

Enregistrer un commentaire