.net 无法加载文件或程序集“System.Data.SQLite”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1278929/
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
Could not load file or assembly 'System.Data.SQLite'
提问by pupeno
I've installed ELMAH 1.1 .Net 3.5 x64 in my ASP.NET project and now I'm getting this error (whenever I try to see any page):
我已经在我的 ASP.NET 项目中安装了 ELMAH 1.1 .Net 3.5 x64,现在我收到此错误(每当我尝试查看任何页面时):
Could not load file or assembly 'System.Data.SQLite, Version=1.0.61.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.BadImageFormatException: Could not load file or assembly 'System.Data.SQLite, Version=1.0.61.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.
无法加载文件或程序集“System.Data.SQLite,Version=1.0.61.0,Culture=neutral,PublicKeyToken=db937bc2d44ff139”或其依赖项之一。试图加载格式不正确的程序。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.BadImageFormatException:无法加载文件或程序集“System.Data.SQLite,Version=1.0.61.0,Culture=neutral,PublicKeyToken=db937bc2d44ff139”或其依赖项之一。试图加载格式不正确的程序。
More error details at the bottom.
底部有更多错误详细信息。
My Active Solution platform is "Any CPU" and I'm running on a x64 Windows 7 on an x64, of course, processor. The reason why we are using this version of ELMAH is because 1.0 .Net 3.5 (x86, which is the only platform for which it's compiled) gave us this same error on our x64 Windows server.
我的活动解决方案平台是“任何 CPU”,我在 x64 处理器上运行 x64 Windows 7,当然,处理器。我们使用这个版本的 ELMAH 的原因是因为 1.0 .Net 3.5(x86,这是编译它的唯一平台)在我们的 x64 Windows 服务器上给了我们同样的错误。
I've tried compiling for x86 and x64 and I get the same error. I've tried removing the all compiler output (bin and obj). Finally I've made a reference to the SQLite dll directly, something that was not needed for the project to work on the server and I've got this compiler error:
我试过为 x86 和 x64 编译,但我得到了同样的错误。我试过删除所有编译器输出(bin 和 obj)。最后,我直接引用了 SQLite dll,这是项目在服务器上运行不需要的东西,我遇到了这个编译器错误:
Error 1 Warning as Error: Assembly generation -- Referenced assembly 'System.Data.SQLite.dll' targets a different processor MyProject
错误 1 警告为错误:程序集生成 - 引用的程序集“System.Data.SQLite.dll”针对不同的处理器 MyProject
Any ideas what the problem might be?
任何想法可能是什么问题?
More error details:
更多错误详情:
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[BadImageFormatException: Could not load file or assembly 'System.Data.SQLite, Version=1.0.61.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0
System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +43
System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +127 System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +142 System.Reflection.Assembly.Load(String assemblyString) +28
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46[ConfigurationErrorsException: Could not load file or assembly 'System.Data.SQLite, Version=1.0.61.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +613 System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +203 System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +105
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +178
System.Web.Compilation.BuildProvidersCompiler..ctor(VirtualPath configPath, Boolean supportLocalization, String outputAssemblyName) +54
System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp) +232
System.Web.Compilation.BuildManager.CompileGlobalAsax() +52 System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +337[HttpException (0x80004005): Could not load file or assembly 'System.Data.SQLite, Version=1.0.61.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +58 System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +512 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters) +729[HttpException (0x80004005): Could not load file or assembly 'System.Data.SQLite, Version=1.0.61.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8896783
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +85
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +259
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。
堆栈跟踪:
[BadImageFormatException:无法加载文件或程序集“System.Data.SQLite,版本=1.0.61.0,Culture=neutral,PublicKeyToken=db937bc2d44ff139”或其依赖项之一。尝试加载格式不正确的程序。]
System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0
System.Reflection.Assembly .nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean for Introspection) +43
System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean for Introspection) +127 System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean for Introspection) +142 System.Reflection.Ass Load(String assemblyString) +28
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46[ConfigurationErrorsException:无法加载文件或程序集“System.Data.SQLite,版本=1.0.61.0,Culture=neutral,PublicKeyToken=db937bc2d44ff139”或其依赖项之一。试图加载格式不正确的程序。]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +613 System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +203 System.Web.Configuration .CompilationSection.LoadAssembly(AssemblyInfo ai) +105
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +178
System.Web.Compilation.BuildProvidersCompiler..ctor(VirtualPath configPath, Boolean supportLocalization, String outputAssemblyName)
System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp) +232
System.Web.Compilation.BuildManager.CompileGlobalAsax() +52 System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +337[HttpException (0x80004005): 无法加载文件或程序集“System.Data.SQLite,版本=1.0.61.0,Culture=neutral,PublicKeyToken=db937bc2d44ff139”或其依赖项之一。尝试加载格式不正确的程序。]
System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +58 System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +512 System.Web.Hosting.HostingEnvironment.Initialize( ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostsParameters) +729[HttpException (0x80004005): 无法加载文件或程序集“System.Data.SQLite,版本=1.0.61.0,Culture=neutral,PublicKeyToken=db937bc2d44ff139”或其依赖项之一。试图加载格式不正确的程序。]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8896783
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +85
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr ) +259
采纳答案by Anton Tykhyy
System.Data.SQLite.dllis a mixed assembly, i.e. it contains both managed code and native code. Therefore a particular System.Data.SQLite.dllis either x86 or x64, but never both.
System.Data.SQLite.dll是一个混合程序集,即它包含托管代码和本机代码。因此,一个特定System.Data.SQLite.dll的要么是 x86 要么是 x64,但绝不是两者兼而有之。
Update (courtesy J. Pablo Fernandez):Cassini, the development web server used by Visual Studio when you press F5 or click the green ?play? button, is x86 only which means that even if your workstation is x64, you'll only be able to use the x86 version of System.Data.SQLite.dll.
更新(由 J. Pablo Fernandez 提供):Cassini,当您按 F5 或单击绿色的“播放?”时 Visual Studio 使用的开发 Web 服务器。按钮,仅是 x86,这意味着即使您的工作站是 x64,您也只能使用 x86 版本的 System.Data.SQLite.dll。
An alternative is not to use Cassini but IIS7 which is properly x64.
另一种选择是不使用 Cassini,而是使用 IIS7,它是正确的 x64。
回答by beckelmw
Make sure that "Enable 32 - Bit Applications" is set to false for the app pool.
确保应用程序池的“启用 32 位应用程序”设置为 false。
回答by umar
Go to the IIS7 Application Pool -> advanced settings and set the 32-bit application to true.
前往IIS7 Application Pool -> advanced settings and set the 32-bit application to true.
回答by Chris
This is very simple if you are not using SQLite:
如果您不使用 SQLite,这非常简单:
You can delete the SQLite DLLs from your solution's bin folders, then from the folder where you reference ELMAH. Rebuild, and your app won't try to load this DLL that you are not using.
您可以从解决方案的 bin 文件夹中删除 SQLite DLL,然后从引用 ELMAH 的文件夹中删除。重建,您的应用程序将不会尝试加载您未使用的此 DLL。
回答by gatapia
I have a 64 bit dev machine and 32 bit build server. I used this code prior to NHibernate initialisation. Works a charm on any architecture (well the 2 I have tested)
我有一台 64 位开发机器和 32 位构建服务器。我在 NHibernate 初始化之前使用了这段代码。适用于任何架构(以及我测试过的 2 个)
Hope this helps someone.
希望这可以帮助某人。
Guido
圭多
private static void LoadSQLLiteAssembly()
{
Uri dir = new Uri(Assembly.GetExecutingAssembly().CodeBase);
FileInfo fi = new FileInfo(dir.AbsolutePath);
string binFile = fi.Directory.FullName + "\System.Data.SQLite.DLL";
if (!File.Exists(binFile)) File.Copy(GetAppropriateSQLLiteAssembly(), binFile, false);
}
private static string GetAppropriateSQLLiteAssembly()
{
string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
string arch = ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, "x86", 0, 3, true) == 0) ? "32" : "64");
return GetLibsDir() + "\NUnit\System.Data.SQLite.x" + arch + ".DLL";
}
回答by Marcos Meli
In our case didn't work because our production server has missing
在我们的情况下不起作用,因为我们的生产服务器丢失了
Microsoft Visual C++ 2010 SP1 Redistributable Package (x86)
Microsoft Visual C++ 2010 SP1 可再发行组件包 (x86)
We installed it and all work fine. The Application Pool must have Enable 32-bit Applications set to true and you must the x86 version of the library
我们安装了它并且一切正常。应用程序池必须将启用 32 位应用程序设置为 true,并且您必须使用 x86 版本的库
回答by Chris S
As someone who's had to deal with quite a few bug reports on Roadkill Wiki with exactly the same issue, this is what you need to do:
作为一个不得不在 Roadkill Wiki 上处理相当多的 bug 报告并遇到完全相同问题的人,这就是您需要做的:
- Are you using x64 or x86? Sqlite comes with DLLs for separate architectures - copy the right one to your bin folder, there are two DLLS for the official provider:
System.Data.SQLite.dllSystem.Data.SQLite.Linq.dll - If you can't be bothered hunting around for these assemblies, enable 32 bit mode for your App Pool (a solution for dev machines only usually)
- If you're hosting on a server, you'll need the Microsoft C++ Runtime redistributable - it's not installed on Server 2008 R2 by default. x64 version, x86 version
- 您使用的是 x64 还是 x86?Sqlite 自带的 DLL 用于单独的体系结构 - 将正确的一个复制到您的 bin 文件夹中,官方提供程序有两个 DLL:
System.Data.SQLite.dllSystem.Data.SQLite.Linq.dll - 如果您懒得四处寻找这些程序集,请为您的应用程序池启用 32 位模式(通常仅适用于开发机器的解决方案)
- 如果您在服务器上托管,则需要 Microsoft C++ Runtime 可再发行组件 - 默认情况下,它不会安装在 Server 2008 R2 上。x64 版本, x86 版本
It's a real pain in the ass how many hoops you have to jump through when re-distributing the SQLite .NET binaries, my solution for Roadkill in the end was to copy the correct binaries to the ~/bin folder based on the architecture your using. Unfortunately that doesn't solve the C++ runtime issue.
在重新分发 SQLite .NET 二进制文件时,你必须跳过多少圈真的很痛苦,我最后对 Roadkill 的解决方案是根据您使用的架构将正确的二进制文件复制到 ~/bin 文件夹. 不幸的是,这并不能解决 C++ 运行时问题。
回答by Kim Th? Ph?m
I resolved this by installing System.Data.SQLite with Nuget extension. This extension can use for Visual Studio 2010 or higher. First, you have to install Nuget extension. You can follow here:
我通过安装带有 Nuget 扩展的 System.Data.SQLite 解决了这个问题。此扩展可用于 Visual Studio 2010 或更高版本。首先,您必须安装 Nuget 扩展。你可以在这里关注:
- Go to Visual Studio 2010, Menu --> Tools
- Select Extension Manager
- Enter NuGet in the search box and click Online Gallery. Waiting it Retrieve information…
- Select the retrieved NuGet Package Manager, click Download. Waiting it Download…
- Click Install on the Visual Studio Extension Installer NuGet Package Manager. Wait for the installation to complete.
- Click Close and 'Restart Now.
- 转到 Visual Studio 2010,菜单 --> 工具
- 选择扩展管理器
- 在搜索框中输入 NuGet,然后单击在线库。等待它检索信息...
- 选择检索到的 NuGet 包管理器,单击下载。等待下载...
- 单击 Visual Studio 扩展安装程序 NuGet 包管理器上的安装。等待安装完成。
- 单击“关闭”和“立即重新启动”。
Second, now, you can install SQLite:
其次,现在,您可以安装 SQLite:
- Go to the menu TOOLS->Library Package Manager->Package Manager Console of the Visual studio.
- Then run the command Install-Package System.Data.SQLite in Package Manager Console. Like this: run the command Install-Package System.Data.SQLite in Package Manager Console
- 转到 Visual Studio 的菜单 TOOLS->Library Package Manager->Package Manager Console。
- 然后在包管理器控制台中运行命令 Install-Package System.Data.SQLite。像这样: 在包管理器控制台中运行命令 Install-Package System.Data.SQLite
And now, you can use System.Data.SQLite.
现在,您可以使用 System.Data.SQLite。
In the case, you see two folder x64 and, x86, these folders contain SQLite.Interop.dll. Now go to the properties windows of those dlls and set build action is content and Copy to output directory is Copy always.
在这种情况下,您会看到两个文件夹 x64 和 x86,这些文件夹包含 SQLite.Interop.dll。现在转到这些 dll 的属性窗口,并将构建操作设置为内容,复制到输出目录为始终复制。
So, that is my way.
所以,这就是我的方式。
Thanks. Kim Tho Pham, HoChiMinh City, Vietnam. Email: [email protected]
谢谢。Kim Tho Pham,越南胡志明市。电子邮件:[email protected]
回答by goku_da_master
I got this error when our windows server was converted from 32 bit OS to 64 bit. The assembly that was throwing the error was set to compile in x86 mode (ie 32 mode). I switched it to "Any CPU" and that did the trick. You can change this value by doing the following:
当我们的 Windows 服务器从 32 位操作系统转换为 64 位时,我收到了这个错误。引发错误的程序集设置为在 x86 模式(即 32 模式)下编译。我将它切换到“任何 CPU”,就成功了。您可以通过执行以下操作来更改此值:
right click on the project go to Properties -> Build -> Platform Target -> change to "Any CPU"
右键单击项目转到 Properties -> Build -> Platform Target -> change to "Any CPU"
回答by lxwwqw
Manual load related System.Data.SQLite assembly can resolve this.
手动加载相关的 System.Data.SQLite 程序集可以解决这个问题。
Changed gatapia's Code as below:
将 gatapia 的代码更改如下:
public static void LoadSQLLiteAssembly()
{
Uri dir = new Uri(Assembly.GetExecutingAssembly().CodeBase);
FileInfo fi = new FileInfo(dir.AbsolutePath);
string appropriateFile = Path.Combine(fi.Directory.FullName, GetAppropriateSQLLiteAssembly());
Assembly.LoadFrom(appropriateFile);
}
private static string GetAppropriateSQLLiteAssembly()
{
string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
string arch = ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, "x86", 0, 3, true) == 0) ? "32" : "64");
return "System.Data.SQLite.x" + arch + ".DLL";
}

