【问题标题】:Unable to load assembly with windows service无法使用 Windows 服务加载程序集
【发布时间】:2013-09-25 21:54:52
【问题描述】:

我在 C# VS2008 中创建了一个 Windows 服务,它使用对外部类库的引用来编写。我在 VS2008 中添加了对它的引用。当我运行启动服务时,它会在尝试访问外部 DLL 时引发异常:

Could not load file or assembly 'vcribAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

DLL 与 service.exe 文件位于同一目录中。对于 Windows 服务,我需要做一些特别的事情吗,比如将 DLL 放在另一个目录中?

【问题讨论】:

  • 使用 Fuslogvw.exe 工具解决此问题。
  • 这与虚拟婴儿床项目有关系吗? thevirtualcrib.com
  • 是的。我是主要开发者之一
  • Aha :) 查看插件示例页面,看起来 api 需要 mysql.data.dll,不是吗? (thevirtualcrib.com/wiki/index.php?title=Create_a_Plugin_Guide)
  • 还有一个问题,您是在开发此服务的同一台机器上运行该服务还是在另一台机器上运行该服务?

标签: c# .net windows-services


【解决方案1】:

可能是 vcribAPI.dll 也依赖于其他程序集。我建议使用Reflector 并打开 dll 以查看它可能引用的其他 dll。

【讨论】:

【解决方案2】:

我遇到了完全相同的错误。

服务的工作目录与应用程序目录不同(一般为C:\Windows\System32)。

例如,如果您尝试定位部署在应用程序目录中的程序集,AssemblyName.GetAssemblyName 方法会抛出 FileNotFoundException

在这种情况下,解决方案是在程序集加载之前使用应用程序目录定义Environment.CurrentDirectory

示例代码:

const string SCHEMA_FILE = @"file:\";
var appAssembly = Assembly.GetExecutingAssembly();
var path = Path.GetDirectoryName(appAssembly.CodeBase);
if (path.StartsWith(SCHEMA_FILE))
    path = path.Remove(0, SCHEMA_FILE.Length);
Environment.CurrentDirectory = path;

【讨论】:

    【解决方案3】:

    您的服务是否有权读取该文件夹?

    【讨论】:

    • 加载 dll 的是 .Net 运行时。如果它可以启动服务 exe,它也将能够从同一文件夹加载 dll。
    【解决方案4】:

    程序集是否有其他依赖的 dll 或程序集?

    如果是这样,它们也需要在这个目录中。

    为了确定,启动Assembly Loader Log(融合日志)。请参阅this howto(调试程序集加载失败)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多