locked
Opening a MS Word document in a windows service seems to hang RRS feed

  • Question

  • I have created a windows service which should access a word document and convert this to a pdf file. To accomplish this I have used Microsoft.Office.Interop.Word . When I created a log file how track the execution of Windows Service. I found the from the point of below code  the application gets hanged.

    Microsoft.Office.Interop.Word.ApplicationClass wordapp =

    newMicrosoft.Office.Interop.Word.ApplicationClass();

    To resolve this I have done the following work around :

    1. Created a "Desktop Folder" in below mentioned locations.

     C:\Windows\System32\config\systemprofile\Desktop
    C:\Windows\SysWOW64\config\systemprofile\Desktop

    2. DMCONFIG -> Computers -> My Computers -> DMCONFIG ->Microsoft Word 97- 2003 - > Properites -> Indentity  : Changed to Interactive user.

    3. From the Project solution took the properties of Microsoft.Office.Interop.Word.dll and set the property "Embed Interop Types" to False.

    But still the while calling this service from a web application. The application gets hanged.

    Will be grateful if any one can help me on this.

    Regards,

    Rahul.


    Tuesday, August 18, 2015 11:35 AM

Answers

  • But still the while calling this service from a web application. The application gets hanged.


    Hi Rahul,

    Did you run the Windows Service application on the server? Please note that Microsoft doesn't recommend users automate the Office on the server side, because there could be many problems, see details about the problems and the corresponding solutions in this KB article:

    https://support.microsoft.com/en-us/kb/257757

    By default, the Office PIA is supposed to be called by desktop-interactive application like Console Application or Winform Application, but not Windows Service application. You can try some workarounds, but I don't recommend that you go in this way:

    https://social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-server-2008-x64?forum=innovateonoffice

    My suggestion is to use some 3rd-party libraries to convert the MSWord documents to PDF files without Office application installed. Search "c# convert word to pdf without office", and you'll find some results.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Caillen Tuesday, September 1, 2015 6:52 AM
    Wednesday, August 19, 2015 8:22 AM
  • The recommended way to do logging for services is to use the Windows Application logs:

    https://msdn.microsoft.com/en-us/library/f6567h1s.aspx

    That you created a folder named Desktop under C:\Windows\System32\config\systemprofile\ makes no real sense. That location is heavily protected by NTFS right limitations. The chance that any programm will actually be able to write there are slim at best. Just naming a folder "Desktop" does not make it easy to access.

    Changing any user away from non-interactive user is a great security risk. Even with that, the services does not automatically inherit interactivitry even when run under the same account:
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683502%28v=vs.85%29.aspx

    A service is not supposed to be interactive. That it was allowed all this time was a serious mistake in Windows. And you can't asume the switch for it will stay around. If you want interactivity, don't use Services.

    • Marked as answer by Caillen Tuesday, September 1, 2015 6:52 AM
    Wednesday, August 19, 2015 10:35 AM
  • My gut instinct is that it is the user account you're running the service under. When you use the interop assembly you are basically automating the app itself. When you start Office the first time it goes through the initialization process of getting your initials and registering. My gut instinct is that the account you're running the service under has never run office and therefore it is hanging on this initialization. Have you tried running your service using a normal user account that you have already run Office at least once with?  This is yet another reason why you should avoid automating Office in a service. 

    You should probably consider a non-Office option like ITextSharp or PDFCreator if possible.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by Caillen Tuesday, September 1, 2015 6:52 AM
    Thursday, August 20, 2015 2:06 PM

All replies

  • But still the while calling this service from a web application. The application gets hanged.


    Hi Rahul,

    Did you run the Windows Service application on the server? Please note that Microsoft doesn't recommend users automate the Office on the server side, because there could be many problems, see details about the problems and the corresponding solutions in this KB article:

    https://support.microsoft.com/en-us/kb/257757

    By default, the Office PIA is supposed to be called by desktop-interactive application like Console Application or Winform Application, but not Windows Service application. You can try some workarounds, but I don't recommend that you go in this way:

    https://social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-server-2008-x64?forum=innovateonoffice

    My suggestion is to use some 3rd-party libraries to convert the MSWord documents to PDF files without Office application installed. Search "c# convert word to pdf without office", and you'll find some results.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Caillen Tuesday, September 1, 2015 6:52 AM
    Wednesday, August 19, 2015 8:22 AM
  • What lead you to use a Windows service for this?
    Wednesday, August 19, 2015 9:50 AM
  • The recommended way to do logging for services is to use the Windows Application logs:

    https://msdn.microsoft.com/en-us/library/f6567h1s.aspx

    That you created a folder named Desktop under C:\Windows\System32\config\systemprofile\ makes no real sense. That location is heavily protected by NTFS right limitations. The chance that any programm will actually be able to write there are slim at best. Just naming a folder "Desktop" does not make it easy to access.

    Changing any user away from non-interactive user is a great security risk. Even with that, the services does not automatically inherit interactivitry even when run under the same account:
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683502%28v=vs.85%29.aspx

    A service is not supposed to be interactive. That it was allowed all this time was a serious mistake in Windows. And you can't asume the switch for it will stay around. If you want interactivity, don't use Services.

    • Marked as answer by Caillen Tuesday, September 1, 2015 6:52 AM
    Wednesday, August 19, 2015 10:35 AM
  • Hi Caillen

    Thanks a lot for the reply.

    Regards,

    Rahul.

    Thursday, August 20, 2015 4:30 AM
  • My gut instinct is that it is the user account you're running the service under. When you use the interop assembly you are basically automating the app itself. When you start Office the first time it goes through the initialization process of getting your initials and registering. My gut instinct is that the account you're running the service under has never run office and therefore it is hanging on this initialization. Have you tried running your service using a normal user account that you have already run Office at least once with?  This is yet another reason why you should avoid automating Office in a service. 

    You should probably consider a non-Office option like ITextSharp or PDFCreator if possible.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by Caillen Tuesday, September 1, 2015 6:52 AM
    Thursday, August 20, 2015 2:06 PM