.net application failing when fired via scheduled task

我只是一个虾纸丫 提交于 2019-12-20 14:43:13

问题


I have a .net console application written in c# which does exactly what it should when run inside visual studio and when clicking the .exe file in the file system. It runs like a charm. BUT when I create a scheduled windows task either in my Windows 7 dev machine or the Windows 2008 R2 prod machine it fails to carry out the last step in the application. This step is to open a .doc in the background and covert it (simply running a save as) to a .docx.

The application is designed to visita network path, create a dir called ~Converted which it does not problem and then for each .doc it finds it opens in background, performs a save as and them saves a .docx version of the do in the ~Converted dir. I believe there is nothing wrong with the code since it builds and runs without issue outside of the scheduled task. I have explored every setting inside the task, made alterations and re-tested with no success. I am running it wit a user who is part of the administrators group, IMO permissions is not the issue. I have tried it with the NETWORK SERVICE account, same result.

The actual stack trace error which is only being generated when run through the scheduled task is:

System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

The line of code which is doing this is:

Application oWord = new Application();

I have search for hours on this and found nothing helpful. It seems like running the code via the task is causing a COM access issue when the code is opening/initializing the word doc. The task is being fun with full admim privileges, running with highest privileges and when the user is logged on or not. As I said all these setting have been played with, it has not made a difference.

It has got to the stage now where I will have to re-write this console application as a service If I cant solve this issue. Hopefully somebody can save me a full days extra work and help me with the issue??

Adding - Based on @Dmitry Martovoi post below I have added ticks for the EVERYONE user and the stack trace has changed to:

System.NullReferenceException: Object reference not set to an instance of an object.

which is pointing to this line inside the same saveAs function:

oDoc.Close(false, Type.Missing, Type.Missing);

Adding - I have been changing users who own this path that much im not sure which user was running it this was generated. here is an application event log erorr. NOTE - This only happens when running the application via task scheduler:

Faulting application name: WINWORD.EXE, version: 14.0.6129.5000, time stamp: 0x5082f340
Faulting module name: btmofficea.dll, version: 3.0.2.280, time stamp: 0x4cc57919
Exception code: 0xc0000005
Fault offset: 0x0000000000005e1f
Faulting process id: 0x2a6c
Faulting application start time: 0x01ce8bec64ccf85d
Faulting application path: C:\Program Files\Microsoft Office\Office14\WINWORD.EXE
Faulting module path: C:\Program Files\Motorola\Bluetooth\btmofficea.dll
Report Id: a393403d-f7df-11e2-b68c-74de2b9ca154

OK problem is fixed. As can be seen in the link @dmay pasted MS do not recommend running office applications unattended on the server side and says this is unsupported. However the solution was down the track @Dmitry was headed:

1) Start->Run->dcomcnfg

2) Component Services->Computers->My Computer

3) Right-click My Computer, Properties

4) COM Security tab, Launch and Activation Permissions, Edit Default

5) Add all access rights for NETWORK SERVICE (which is the best account to use apparently), you are using to run scheduled task

6) Right-click My Computer > DCOM Config > Microsoft Word 97 - 2003 Document > Properties

7) Go to identity Tab and tick the interactive user

8) Go to Security tab and check the customize settings for all three sections. Make sure NETWORK SERVICE shows and has options ticked. This should be as a result of step 5.

9) Repeat from step 6 for other office document types. I have done so for xls and mdb

10) restart....All are working well now

Sorry I should have added this. If you can not see these at step 7:

               'Microsoft Word 97 - 2003 Document'

               'Microsoft Excel Application'

               'Microsoft Access Application

Then it probably a 32bit office installation on a 64 bit machine, here is the solution to make them visible:

Thank you everyone for your time


回答1:


You should grant to the specified user access to COM automation factory:

  • 1) Start->Run->dcomcnfg
  • 2) Component Services->Computers->My Computer
  • 3) Right-click My Computer, Properties
  • 4) COM Security tab, Launch and Activation Permissions, Edit Default
  • 5) Add all access rights for the specified user, you are using to run scheduled task.



回答2:


Faced with similiar problem, when application normally is worked as expected, but when i run it from the scheduled task, it's immediately closing, and throws an error: "File path not found ...".

Fixed it with setting scheduled task action parameter:

Start In (optional): c:\my application\executable folder path




回答3:


It seems like a permissions issue to me. Running the code inside VS is way too different to running it on its own. VS takes care of most of the stuff and makes life easy for the developer.

Can you please check if the account you are using to run the scheduled task has permission to access the network path?




回答4:


The first of all, take a look on this http://support.microsoft.com/?id=257757

You can try few things:

  • Run you task under real user account (make sure this account has access to network path)
  • 'Ogawa Hack', as mentioned here http://social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-server-2008-x64 create desktop folder for system account C:\Windows\SysWOW64\config\systemprofile\Desktop (or \System32\ if your system is 32x)


来源:https://stackoverflow.com/questions/17913670/net-application-failing-when-fired-via-scheduled-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!