简体   繁体   English

C# 服务与 Interop.Excel 不能作为服务工作,但只能在本地工作

[英]C# Service with Interop.Excel not working as a service but only locally

I need help with a service in C# I am fixing.我需要有关 C# 服务的帮助,我正在修复。 This service does (in order):该服务(按顺序):

  1. copies a XLSM file from a NAS folder in local WM;从本地 WM 中的 NAS 文件夹复制 XLSM 文件;
  2. parse this file using Microsoft.Office.Interop.Excel (vers. 15.0.4795.1000 from NuGET);使用 Microsoft.Office.Interop.Excel 解析此文件(来自 NuGET 的 15.0.4795.1000 版);
  3. fill a table in a DB with data extracted from this Excel file.使用从此 Excel 文件中提取的数据填充数据库中的表。

This service is written with C# in VS2019 in the same VM where it should run.该服务使用 VS2019 中的 C# 编写,位于应运行的同一 VM 中。 If I run it in debug mode with VS everything works, so I installed it in the VM but now it does not work anymore, I keep getting this error as soon as step 2 starts:如果我在调试模式下使用 VS 运行它一切正常,所以我将它安装在 VM 中,但现在它不再工作,一旦第 2 步开始,我就会不断收到此错误:

System.Exception: System.Runtime.InteropServices.COMException (0x800A03EC): Cannot access file XXXX.xslm System.Exception:System.Runtime.InteropServices.COMException(0x800A03EC):无法访问文件 XXXX.xslm

The file XXXX.xlsm is the file copied from NAS in the local VM and is opened in read only mode; XXXX.xlsm文件是本地VM中从NAS复制的文件,以只读方式打开; so step 1 is correctly executed.所以步骤1正确执行。 The service runs with the same user I use to log in the VM.该服务使用我用来登录 VM 的同一用户运行。

It it may help, in this VM Excel 2019 is installed, and anyway it works when I run in debug mode.它可能会有所帮助,在这个 VM Excel 2019 已安装,无论如何,当我在调试模式下运行时它可以工作。

Can anybody help me?有谁能够帮我?

Are you using absolute path to access to to file?您是否使用绝对路径访问文件?

Usually service execution folder is different than the execution folder of an normal application and depends on the execution context of the service.通常服务执行文件夹不同于普通应用程序的执行文件夹,并且取决于服务的执行上下文。

If you have to use relative path try with:如果您必须使用相对路径,请尝试:

AppDomain.CurrentDomain.BaseDirectory

To find the right path.寻找正确的道路。

If path is ok check that the service account user has valid rights to access and (if you need that) edit the file.如果路径正常,请检查服务帐户用户是否具有有效的访问权限并(如果需要)编辑文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM