Search code examples
c#outlookvstooutlook-addinword-addins

Scope of static class or variable in Shared office Addin


We have a Static Map in shared addin (Word, Outlook) to store some cache information while application is running as follows:

public static class GlobalVariables
{
    //Key constants
    public static string USER_ACTION = "userAction";

    //private methods
    private static Dictionary<string, Object> globalMap = new Dictionary<string, object>();

    public static Dictionary<string, Object> GlobalMap
    {
        set { globalMap = value; }
        get { return globalMap; }
    }

    public static Object get(string key)
    {
        // custom Implementation
    }

    public static void Add(string key, Object obj)
    {
       //custom implementation
    }
}

We have a use case where We use Outlook for sending mail and perform some operation on mail send as follows:

private void adxOutlookAppEvents_ItemSend(object sender, ADXOlItemSendEventArgs e)
    {
        try
        {
            Outlook._MailItem mailItem = e.Item as Outlook._MailItem;
            string action = (string)GlobalVariables.get(GlobalVariables.USER_ACTION);
            if (false == string.IsNullOrEmpty(action))
            {
                this.performUserActionAfterMailSend(action);

        }
        catch (CustomeMessageException ex)
        {
            CommonUtils.iAlert(ex.Message);
        }
        catch (Exception ex)
        {
            CommonUtils.iError(RESOURCE.COMMON_ERROR_MSG);
        }
        finally
        {
            //CommonUtils.releaseComObject(oRecips);
        }

    }

But in adxOutlookAppEvents_ItemSend() when I try to get GlobalMap it returns null. As far as I know Static variable are accessible across threads. Is It because inter application communication? Why GlobalMap return null when I try to access it?

I would appreciate any help


Solution

  • COM addins are implemented as dlls loaded by each host process (outlook.exe, msword.exe, etc.). No classes are ever shared between two running processes. You need to come up with a different storage mechanism (registry? file system?) to share common data.