Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有后台工作程序的windows服务无法加载文件或程序集错误_C#_.net_Windows Services_Assembly Resolution_Assemblybinding - Fatal编程技术网

C# 具有后台工作程序的windows服务无法加载文件或程序集错误

C# 具有后台工作程序的windows服务无法加载文件或程序集错误,c#,.net,windows-services,assembly-resolution,assemblybinding,C#,.net,Windows Services,Assembly Resolution,Assemblybinding,我正在创建一个windows服务,它测试是否可以执行计划的作业,然后启动后台工作程序来执行作业 在windows服务的主线程中,我从与windows服务可执行文件位于同一目录中的程序集创建了一个数据访问层(DAL)对象。 这很有效 在后台工作程序中,我还尝试创建同一对象的新实例。看来这是成功的。DAL中的一个方法从程序集中加载一个SQL文件,然后针对给定数据库执行 在这个过程中,我发现了以下错误: System.IO.FileLoadException: Could not load file

我正在创建一个windows服务,它测试是否可以执行计划的作业,然后启动后台工作程序来执行作业

在windows服务的主线程中,我从与windows服务可执行文件位于同一目录中的程序集创建了一个数据访问层(DAL)对象。 这很有效

在后台工作程序中,我还尝试创建同一对象的新实例。看来这是成功的。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\目录。

这可能会解决问题:

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

DAL中的一个方法从程序集中加载一个SQL文件-这意味着什么?SQL文件是否编译到程序集中?-1这不是由编码问题引起的-问题在于程序集绑定解析。@TomRedfern。我怀疑这是一个编码问题。服务的当前目录将是c:\windows\system32,因此我怀疑程序(使用Assembly.LoadFrom)只是在当前目录中查找dll。如果是这样,那么这可能会解决问题。另一种方法是将Assembly.LoadFrom更改为从正确的目录加载文件。在根据@sgmoore的评论重新阅读问题后,我承认我最初对coke head答案的评估是错误的。抱歉不幸的是,除非你更新你的答案,否则我无法撤消我的否决票…@TomRedfern-没问题。伙计,当我看到反对票时,我想我一定是误读了米沙的问题。无论如何,我更新了我的答案。