Link to home
Start Free TrialLog in
Avatar of mcorrente
mcorrenteFlag for United States of America

asked on

CDO and Windows Service

I have a windows service that I'm installing on a server.  The service uses CDO to iterate through mail items in a public folder.  The CDO code works fine in a windows forms application that I've been using to debug it.  The service works fine as far as starting, stopping and writing to the event log.

However, when I put the CDO related code into the service, it's as if it is completely ignored.  No error messages, no unexpected service stops or anything.  It just doesn't perform.

For instance:  I had the code I needed executed on the elapsed event for a timer.  On elapsed, the service would write a test entry to the event log, then try to access the public folder, then write the number of messages in the folder to the event log.  Neither eventlog entry was added.  However, when I moved the CDO code into a separate procedure and called it from the OnElapsed event (leaving the first event log entry in the event itself) the first entry would be added to the event log, and then nothing.  The service would continue to run, the timer continues to loop adding the initial event log every ten seconds, but the CDO code never goes anywhere.

Any ideas?
Avatar of mastoo
mastoo
Flag of United States of America image

You might try having the service run under your account once.  If that works then you know it is a problem related to the service's account.  Also, attach a debugger to it and step through the code.
Avatar of mcorrente

ASKER

Ok, got an error:

COMException was unhandled by user code
Retrieving the COM class factory for component with CLSID {3FA7DEB3-6438-101B-ACC1-00AA00423326} failed due to the following error: 80010106.

Occurs on line:
Dim mapiSession as New MAPI.Session.  

This works fine in a windows form application.  The CDO library is referenced correctly.  No build errors.
ASKER CERTIFIED SOLUTION
Avatar of mastoo
mastoo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried error handling in the procedure, but it ignores that, too.  I put an line of code to create a log entry when an error is raised, but the log entry is never created.

I am trying this under my account.  Still no go.

I attempted to force STA, but still had the error.
Oh, wait oh wait....

STA worked.  I moved up the STAThread attribute up one level to the actual timer elapsed event (rather than the procedure I was calling in the event) and it seems to be working.

Very cool.

Thanks for the help, mastoo.

FOR FUTURE TROUBLESHOOTERS:
I'm working in VB.  I added "<System.STAThread()>" immediately before the procedure declaration (on the same line)  for the procedure with the COM reference.  Here:

    <System.STAThread()> Private Sub MyTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
        Me.evlMyCustomEventLog.WriteEntry("Attempting Connect")
        ElapsedEventCode()
    End Sub

'ElapsedEventCode is a separate procedure that actually contains the COM reference and runs the code I want run on the timer elapsed event.  I moved it out of the actual event when I was trying to fix this problem, but it could probably be put right back there.
Adding a couple search phrases that might help people find this solution in the future:

new mapi.session
exchange
outlook