.net – Could not load file or assembly ‘System.Data.SQLite’

elmahnetsqlite

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):

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.

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.

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:

Error 1 Warning as Error: Assembly generation — Referenced assembly 'System.Data.SQLite.dll' targets a different processor 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

Best Answer

System.Data.SQLite.dll is a mixed assembly, i.e. it contains both managed code and native code. Therefore a particular System.Data.SQLite.dll is either x86 or x64, but never both.

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.

An alternative is not to use Cassini but IIS7 which is properly x64.