C# 无法加载文件或程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/936355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:42:54  来源:igfitidea点击:

Could not load file or assembly

c#.netnhibernate

提问by Dathan

I'm working on a system to use a SqlServerCewith NHibernate. From my driver program, if I add the System.Data.SqlServerCeassembly as a reference, I can create and run queries against a database just fine. When trying to use NHibernate, though, I get the following exception:

我正在研究一个使用SqlServerCewith的系统NHibernate。从我的驱动程序中,如果我添加System.Data.SqlServerCe程序集作为参考,我可以很好地创建和运行对数据库的查询。NHibernate但是,在尝试使用时,我收到以下异常:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Additional information: Could not load file or assembly 'System.Data.SqlServerCe' or one of its dependencies. The system cannot find the file specified.
mscorlib.dll 中出现了“System.IO.FileNotFoundException”类型的第一次机会异常附加信息:无法加载文件或程序集“System.Data.SqlServerCe”或其依赖项之一。该系统找不到指定的文件。

I've traced the exception to a call to Assembly.Load("System.Data.SqlServerCe"), which seems like it should work. The System.Data.SqlServerCeassembly is in the GAC (I've also tried to add it as a local reference with CopyLocal=true, to no avail), and I can use its members fine, so why can't I explicitly load it? When I open the assembly in Reflector, it has trouble loading the System.Transactionsreference (I've also tried adding it as a local reference, again to no avail), so loading that assembly might be the problem, rather than the System.Data.SqlServerCe assembly.

我已经将异常追溯到调用Assembly.Load("System.Data.SqlServerCe"),这似乎应该可以工作。该System.Data.SqlServerCe程序集在 GAC 中(我也尝试将其添加为本地引用CopyLocal=true,但无济于事),我可以很好地使用其成员,那么为什么我不能明确加载它呢?当我在 Reflector 中打开程序集时,加载System.Transactions引用时遇到问题(我也尝试将其添加为本地引用,但同样无济于事),因此加载该程序集可能是问题所在,而不是System.Data.SqlServerCe assembly.

Is this a common problem? System misconfiguration, maybe?

这是个常见的问题吗?系统配置错误,可能吗?

采纳答案by Dathan

Apparently this can be solved by adding a <qualifyAssembly> element to the app.config file. Adding the following has my app running smoothly:

显然,这可以通过向 app.config 文件中添加 <qualifyAssembly> 元素来解决。添加以下内容使我的应用程序运行顺利:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <qualifyAssembly partialName="System.Data.SqlServerCe" fullName="System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </assemblyBinding>
</runtime>

Thanks!

谢谢!

回答by Bhushan

I had a similar problem. The mistake I was doing was I was trying to execute the .exe present in the ../obj/x86/Release, whereas I am supposed to execute the .exe present in ../bin/Release. (I am absolute newbie to C#).

我有一个类似的问题。我犯的错误是我试图执行 ../obj/x86/Release 中的 .exe,而我应该执行 ../bin/Release 中的 .exe。(我绝对是 C# 的新手)。

(I also noticed that in this ../bin/Release directory , the referenced .dll file is copied locally.)

(我还注意到在这个 ../bin/Release 目录中,引用的 .dll 文件被复制到本地。)

回答by rob

This is most probably connected to some system (mis)configuration.

这很可能与某些系统(错误)配置有关。

However, by design SQL Server CE is just a single DLL, which may be shipped together with your product.
This means that you can just set Copy localto Truein the reference properties of System.Data.SqlServerCe, and you are done.

但是,按照设计,SQL Server CE 只是一个单独的 DLL,它可能与您的产品一起提供。
这意味着您只需在 的引用属性中设置Copy localTrueSystem.Data.SqlServerCe就完成了。