简体   繁体   English

无法加载文件或程序集

[英]Could not load file or assembly

I'm working on a system to use a SqlServerCe with NHibernate .我正在研究一个将SqlServerCeNHibernate一起使用的系统。 From my driver program, if I add the System.Data.SqlServerCe assembly as a reference, I can create and run queries against a database just fine.从我的驱动程序中,如果我添加System.Data.SqlServerCe程序集作为参考,我可以很好地创建和运行数据库查询。 When trying to use NHibernate , though, I get the following exception:但是,在尝试使用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. mscorlib.dll 中发生了“System.IO.FileNotFoundException”类型的第一次机会异常附加信息:无法加载文件或程序集“System.Data.SqlServerCe”或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。

I've traced the exception to a call to Assembly.Load("System.Data.SqlServerCe") , which seems like it should work.我已经将异常追溯到对Assembly.Load("System.Data.SqlServerCe")的调用,这似乎应该可以工作。 The System.Data.SqlServerCe assembly 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? System.Data.SqlServerCe程序集在 GAC 中(我也尝试将其添加为使用CopyLocal=true的本地引用,但无济于事),我可以很好地使用其成员,那么为什么我不能显式加载它? When I open the assembly in Reflector, it has trouble loading the System.Transactions reference (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 .当我在 Reflector 中打开程序集时,加载System.Transactions引用时遇到问题(我也尝试将其添加为本地引用,但同样无济于事),因此加载该程序集可能是问题所在,而不是System.Transactions引用System.Data.SqlServerCe assembly

Is this a common problem?这是个常见的问题吗? System misconfiguration, maybe?系统配置错误,可能吗?

Apparently this can be solved by adding a <qualifyAssembly> element to the app.config file.显然,这可以通过向 app.config 文件中添加 <qualifyAssembly> 元素来解决。 Adding the following has my app running smoothly:添加以下内容使我的应用程序运行顺利:

<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!谢谢!

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.但是,按照设计,SQL Server CE 只是一个单独的 DLL,它可能与您的产品一起提供。
This means that you can just set Copy local to True in the reference properties of System.Data.SqlServerCe , and you are done.这意味着您只需在System.Data.SqlServerCe的引用属性中将Copy local设置为True

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.我犯的错误是我试图执行 ../obj/x86/Release 中的 .exe,而我应该执行 ../bin/Release 中的 .exe。 (I am absolute newbie to C#). (我绝对是 C# 的新手)。

(I also noticed that in this ../bin/Release directory , the referenced .dll file is copied locally.) (我还注意到在这个 ../bin/Release 目录中,引用的 .dll 文件被复制到本地。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM