Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:50:10+00:00 2026-06-17T18:50:10+00:00

Ok, it seemed like a good idea to make myself a MailService class as

  • 0

Ok, it seemed like a good idea to make myself a MailService class as a singleton to work with the Outlook class (which itself is a singleton afaik).

It worked great at first, but as soon as I tried setting up unit tests (MS Unit Testing) for this class from my appservice layer all hell broke loose and I get Runtime Callable Wrapper errors..

Was I wrong setting up a MailService class as a singleton or am I doing something wrong in wrapping Outlook as singleton class?

This is my Mailservice class with one of the methods shown:

public class MailService : IDisposable
{

    private static MailService _instance;
    private Outlook.ApplicationClass _app;
    private Outlook.NameSpace _olNS;

    private MailService()
    {
        _app = new Outlook.ApplicationClass();
        _olNS = _app.GetNamespace("MAPI");
    }

    public static MailService Instance
    {
        get
        {
            if (_instance == null)
                _instance = new MailService();
            return _instance;
        }
    }

    public void Dispose()
    {
        _app.Quit();
        Marshal.ReleaseComObject(_app);
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    public Outlook.Folder getFolderByPath(string sFolderPath)
    {
        Outlook.Folder olFolder = null;
        if (sFolderPath.StartsWith(@"\\"))
        {
            sFolderPath = sFolderPath.Remove(0, 2);
        }
        string[] sFolders = sFolderPath.Split('\\');
        try
        {
            olFolder = _app.Session.Folders[sFolders[0]] as Outlook.Folder;
            if (olFolder != null)
            {
                for (int i = 1; i <= sFolders.GetUpperBound(0); i++)
                {
                    Outlook.Folders subFolders = olFolder.Folders;
                    olFolder = subFolders[sFolders[i]] as Outlook.Folder;
                    if (olFolder == null)
                    {
                        return null;
                    }
                }
            }
            return olFolder;
        }
        catch (Exception e)
        {
            LogHelper.MyLogger.Error("Error retrieving " + sFolderPath, e);
            return null;
        }
    }
}

My MS Unit tests are working separately but not when running all in a test list. In the latter condition only the first Test passed…

    [TestMethod]
    public void TestMonitorForCleanUpDone()
    {
        Assert.IsNotNull(MailService.Instance.getFolderByPath(olFolderDone));
    }

    [TestMethod]
    public void TestMonitorForCleanUpIn()
    {
        Assert.IsNotNull(MailService.Instance.getFolderByPath(olFolderIn));
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:50:12+00:00Added an answer on June 17, 2026 at 6:50 pm

    As usual it turns out someone else has faced this problem before and wrote a blog post about it:
    http://blogs.msdn.com/b/martijnh/archive/2009/12/31/unit-testing-com-object-that-has-been-separated-from-its-underlying-rcw-cannot-be-used.aspx

    Just in case the blogpost ever goes away, the resolution in short is to run Unit Tests in MTA mode.

    “the way to solve this issue is to open up your testrunconfig file using the XML editor and adding the <ExecutionThread apartmentState=”MTA” /> element to it”

    <?xml version="1.0" encoding="utf-8"?>
    <TestRunConfiguration name="Local Test Run" id="f3322344-5454-4ac5-82b7-fa5ba9c1d8f2"     xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
      <ExecutionThread apartmentState="MTA" />
      <Description>This is a default test run configuration for a local test run.</Description>
      <TestTypeSpecific />
    </TestRunConfiguration>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Earlier on, I committed some code which at the time seemed like a good
GenICam seemed like the best alternative, however the documentation is sparse. Thanks in advance!
This seemed like it should be easy, but I have had trouble getting it
I was thinking about using memcached to store sessions instead of mySQL, which seemed
When is it a good idea to use more than one DBMS? What are
This seemed to spark a bit of conversation on another question and I thought
I seemed to have run into a strange problem with the CCParticleSystem and the
This seemed pretty straightforward: capture a POST in Fiddler (Windows, because I find it
My script works seemed to be working perfectly well, but Eclipse 3.7.2 was telling
I saw somewhere what seemed to be nested selects, one master select on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.