You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The manual binding of a subscriber has the same scope as the variable instance that it is being used in the BindSubscription() call. The binding is active as long as the codeunit instance is active, or until a UnbindSubscription() is called on the instance.
In below sample, the event subscribers in SingleInstanceSubscribers are still active after the error has been thrown, leaving them active until end of session (or if some other code is calling UnbindSubscription(MySingleInstanceSubs))
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
According to https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/performance/performance-developer#limit-your-event-subscriptions, among others, it is recommended to have event subscriber codeunits in SingleInstance codeunits.
Use single instance codeunits for subscribers, if possible.
In the same paragraph, it also says that
Static automatic has a cost over manually binding
.But combining those two in the same codeunit like this could have unexpected behavior:
The manual binding of a subscriber has the same scope as the variable instance that it is being used in the
BindSubscription()
call. The binding is active as long as the codeunit instance is active, or until aUnbindSubscription()
is called on the instance.Example 1 - normal codeunit
In below sample, the subscribers in MySubs is inactive after the error is thrown.
Example 2 - SingleInstancecodeunit
In below sample, the event subscribers in
SingleInstanceSubscribers
are still active after the error has been thrown, leaving them active until end of session (or if some other code is callingUnbindSubscription(MySingleInstanceSubs)
)Suggestion for new rule
Please add a new rule that warns if
EventSubscriberInstance = Manual;
andSingleInstance = true;
is set in the same codeunit.There are some scenarios where above setup is OK, but pragmas should be ok to use in those cases.
Beta Was this translation helpful? Give feedback.
All reactions