我在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放在另一个目录中吗?
答案 0 :(得分:1)
可能vcribAPI.dll也依赖于其他程序集。我建议使用Reflector并打开dll以查看它可能引用的其他dll。
答案 1 :(得分:1)
我遇到了完全相同的错误。
服务的工作目录与应用程序目录(通常为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;
答案 2 :(得分:0)
答案 3 :(得分:0)
您的服务是否有权读取该文件夹?