In our code we have hundreds of OOM accesses that we wrap by redemption safe*objects in order to avoid security warnings.
To simplify our code we create a redemption Safe*Object for each property/method access by using extension methods:
internal static class OutlookSafeExtensions
{
private static SafeMailItem ToSafe(this MailItem mailItem)
{
var sMailItem = new SafeMailItem();
sMailItem.Item = mailItem;
return sMailItem;
}
}
// usage:
var body = mail.ToSafe().Body;
Are there any risks or performance issues in outlook redemption when creating Safe*Objects very often?
That would work, but creating and destroying hundreds of Redemption objects can be a potential problem - each time a Redemption object is created, it calls MAPIInitialize
. When destroyed - MAPIUninitialize
. That can result in the MAPI system being initialized and uninitialized many times, which can be a problem after 255 or so iterations. This won't be a problem in a COM addin where MAPI is initialized by Outlook, but if you are in an external app, you can run over the limit.
The solution is to create a Redemption object on your app startup and keep it initialized for the lifetime of your app. That will ensure that the MAPI system is initialized and stays initialized.
For your particular extension class, you can introduce a static field that is initialized the first time the class is used.
internal static class OutlookSafeExtensions
{
private static RDOSession _session = RedemptionLoader.new_RDOSession();
...
}