0

我正在創建一個Windows服務,它測試是否可以執行預定作業,然後啓動後臺工作來執行作業。與後臺工作者的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 \目錄下。

+0

*在DAL的方法從組件*加載一個SQL文件 - 這是什麼意思? SQL文件是否被編譯到程序集中? – 2014-10-17 09:30:12

回答

2

這可能會解決這個問題:

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

-1這不是由編碼問題引起的 - 問題在於程序集綁定解析。 – 2014-10-17 09:37:24

+0

@TomRedfern。我懷疑這**是一個編碼問題。一個服務的當前目錄將是c:\ windows \ system32,因此我懷疑程序(使用Assembly.LoadFrom)只是在當前目錄中查找dll。如果是這樣,那麼這可能會解決這個問題。另一種方法是更改​​Assembly.LoadFrom以從正確的目錄加載文件。 – sgmoore 2014-10-17 09:52:51

+0

根據@ sgmoore的評論重新閱讀這個問題後,我承認在我最初對可口可樂回答的評估中我錯誤了。道歉! - 不幸的是我不能撤消我的投票,除非你更新你的答案... – 2014-10-17 10:08:48

相關問題