我正在创建一个Windows服务,它测试是否可以执行预定作业,然后启动后台工作程序来执行作业。
在Windows服务的主线程中,我从与Windows服务可执行文件位于同一目录中的程序集创建数据访问层(DAL)对象。 这很有效。
在后台worker中,我也尝试创建同一个对象的新实例。看起来这是成功的。 DAL中的方法从程序集加载SQL文件,然后针对给定数据库执行。
在此过程中某处我收到以下错误:
System.IO.FileLoadException: Could not load file or assembly 'file:///C:\Windows\system32\DataConnector.dll' or one of its dependencies. The system cannot find the file specified.
File name: 'file:///C:\Windows\system32\DataConnector.dll'
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at DataConnector.DatabaseConnector.UpdateDatabase()
我不知道为什么后台工作者试图查看C:\ windows \ system32 \目录。
答案 0 :(得分:3)
这可能会解决问题:
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);