I have created a windows service in C# VS2008 that uses a reference to an external class library to wrote. I have added the reference to it in VS2008. When I run start the service it throws an exception when trying to access the external DLL:
Could not load file or as开发者_如何学Pythonsembly 'vcribAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
The DLL is in the same directory as the service.exe file. Is there something special that I need to do for windows services like putting the DLL in another directory?
It could be that vcribAPI.dll relies on other assemblies as well. I suggest using Reflector and open up the dll to see what other dll's it might reference.
I encountered exactly the same error.
The working directory of services is different from the application directory (typically C:\Windows\System32).
For example, the method AssemblyName.GetAssemblyName
throws a FileNotFoundException
if you try to locate an assembly deployed in the application directory.
In this case, the solution is to define Environment.CurrentDirectory
with the application directory before assembly loading.
Sample code :
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;
Does you service have rights to read in the folder?
Does the assembly have other dlls or assemblies it depends on?
If so, they also need to be in this directory.
To be certain, start up the Assembly Loader Log (fusion log). See this howto (Debugging Assembly Loading Failures).
精彩评论