Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to find some answers on why service code, that I have built accessing Outlook using MAPI, works fine when it is being debugged within Visual Studio, but when the service is deployed, the call that gets the inbox, hangs. There is no timeout. The service just hangs.

Specifics:
1. Segment of code:
Application outlook = new Application();
NameSpace ns = outlook.GetNamespace("MAPI")
Folder folder = ns.GetDefaultFolder(olDefaultFolder.olFolderInbox) as Folder;

2. Outlook 2019 is installed on my PC.

3. Debugging code works fine. But, when I try to deploy this code a as a Windows Service, on my local machine, it will hang on the GetDefaulFolder. There is no return.

4. Project is a Window Service using the 4.7 .NET framework.

Has anyone else seen a similar issue? Thank you in advance for the help or suggestions.

What I have tried:

I have tried the following:
1. Adding impersonation around the code, even though the service is using my credentials.
2. FYI... I have added the STAThread attribute onto the parent method.
3. I have tried the code:
Application outlook = new Application();
Folder folder = outlook.Session.GetDefaultFolder(olDefaultFolder.olFolderInbox) as Folder;
4. Looked at properties on my local outlook. Nothing would cause this strange difference in behavior, even though it does seem to be some form of access or authentication where debugging resolves the issue.
5. Reviewed properties within the Administration screen - MAPI is enabled. But, then, it all works fine when I am debugging in Visual Studio, just not when deployed as a wondows service.
6. I have tried different threading models (i.e. Task.Factory, Timer,etc) , and each work fine when debugging within Visual Studio. I am currently using the standard BackGoundWorker class (i.e. BackgroundWorker bk = new BackgroundWorker(); bk.DoWork += MyServiceMethod(); bk.RunWorkerAsync();
Posted
Updated 20-Sep-22 10:51am

That's because you cannot use Office Interop in a Service or Web app. Office does not support running from a non-user interactive session.

When running your code under the debugger, the code is running as your account, but when running as a Service, by default, it's running as LocalMachine, a non-user account.
 
Share this answer
 
Hi Dave,

Thank you for your response.

The service account is not running under default. In my dev environment, I have it set to my user AD account. So, the service is running as if it was me.

However, I did find the issue. Outlook has a setting that blocks activity and requires user input. When running the service, that dialog does not show but, instead, blocks the service from continuing. Works as coded... Once I set the "Never warn me about suspicious activity" within the Trust Center of Outlook (under Programmatic Access), it worked. I believe I need to add my new service as a trusted application within antivirus. Everything is working now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900