Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly

I'm working on a system to use a SqlServerCe with NHibernate. 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. When trying to use NHibernate, though, I get the following exception:

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.

I've traced the exception to a call to Assembly.Load("System.Data.SqlServerCe"), which seems like it should work. 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? 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.

Is this a common problem? System misconfiguration, maybe?

like image 830
Dathan Avatar asked Jun 01 '09 19:06

Dathan


People also ask

How do you fix Could not load file or assembly or one of its dependencies?

There are some workarounds for this issue: The dll file may not be in /bin folder. Just copy the dll file to /bin folder or set this preference Copy Local = True from Visual Studio. If the problem persists, check if the version of the assembly that is referenced is different than the version it is looking for.

Could not load file or assembly causes?

In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.

Could not load file or assembly attempt was made to load a program with an incorrect format?

“An attempt was made to load a program with an incorrect format.” That means that the assembly, which was to be loaded, was in an unexpected format. The format, in this case, refers most likely to the 64-bit build of an application being deployed to IIS, which is being run in 32-bits.

Could not load file or assembly Publickeytoken null or one of its dependencies?

This error usually means that the assembly was not found. Try verifying that the file exists in the directory where your application is running.


2 Answers

Apparently this can be solved by adding a <qualifyAssembly> element to the app.config file. 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!

like image 149
Dathan Avatar answered Sep 27 '22 03:09

Dathan


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 local to True in the reference properties of System.Data.SqlServerCe, and you are done.

like image 27
rob Avatar answered Sep 26 '22 03:09

rob