Could NOT LOad file or assembly – Part 1


The error message “Could not load file or assembly” can be quite a common error in ASP.NET applications. This is because it can occur for a whole plethora of reasons and this in itself can make it a bit of a tricky problem to solve.

Of course in some cases it could be competely trivial – you just forgot to deploy one of the assemblies that your application depends on to the BIN folder on your production server, that kind of thing.

However sometimes the cause is less obvious.

So what does the error look like in full? You would probably see something more akin to this:

System.IO.FileNotFoundException: Could not load file or assembly ‘MyLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0e1d67af9d31f077’ or one of its dependencies. The system cannot find the file specified.
File name: ‘MyLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0e1d67af9d31f077’ —> System.IO.FileNotFoundException: Could not load file or assembly ‘MyLibrary’ or one of its dependencies. The system cannot find the file specified.
File name: ‘MyLibrary’

 

This might occur when you are explicitly trying to load an assembly (for example by calling Assembly.Load) or more likely it occurs indirectly as a result of something else your application does, like trying to display a particular web control for example.

 

Now there are some known problems that can lead you to get this error and here are a few examples:

 

915782 FIX: You may receive an InvalidCastException error in an ASP.NET-connected Web application
http://support.microsoft.com/default.aspx?scid=kb;EN-US;915782

919825 You may receive an error message that is misleading when you generate a delay-signed ActiveX wrapper in a Visual C# Windows application project in Visual Studio 2005
http://support.microsoft.com/default.aspx?scid=kb;EN-US;919825

820126 BUG: New Interop DLL Is Not Put in the Correct Location for the Project When a COM Component Is Added
http://support.microsoft.com/default.aspx?scid=kb;EN-US;820126

823196 PRB: You Receive a “System.IO.FileNotFoundException” Error When the Client Application Calls a Web Service
http://support.microsoft.com/default.aspx?scid=kb;EN-US;823196

 

As you can see there is quite a variety of situations and causes for this and similar errors. Some of the situations are pretty specific.

Some tools that can be helpful in diagnosing the cause of such failures are FUSLOGVW.EXE (that comes with the Microsoft .NET SDK) and also the trusty old favourite FileMon.

 

The other day I was assisting a customer with a situation where an ASP.NET 1.1 application that had worked perfectly well for quite some time gave a “file not found” error when configured to run with ASP.NET 2.0.  After examining the application and its configuration files and discussing the problem with her I constructed a small sample application (outside of ASP.NET) to focus on the application’s use of bindingRedirect.

I compiled the EXE along with two versions of MyLibrary.dll using Visual Studio .NET 2003 and ran the application using this configuration file:

<configuration>
<startup>
<requiredRuntime version=”v1.1.4322″ safemode=”true”/>
</startup>
<runtime>
<assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1″>
<qualifyAssembly partialName=”MyLibrary” fullName=”MyLibrary,version=1.0.0.0,publicKeyToken=0e1d67af9d31f077,culture=neutral” />
<dependentAssembly>
<assemblyIdentity name=”MyLibrary” publicKeyToken=”0e1d67af9d31f077″ culture=”neutral” />
<bindingRedirect oldVersion=”1.0.0.0-1.0.0.0″ newVersion=”1.0.0.1″ />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

All the EXE did was to do Assembly.Load(“MyLibrary”) and display the version of MyLibrary. With this configuration file it should realise that when trying to load version 1.0.0.0 of MyLibrary.dll, the version the EXE was referencing at compilation time, the loader should instead load version 1.0.0.1.

This worked and told me that version 1.0.0.1 of MyLibrary had been loaded.

I then changed

<requiredRuntime version=”v1.1.4322″ safemode=”true”/>

to

<requiredRuntime version=”v2.0.50727″ safemode=”true”/>

so that the 2.0 runtime would be used and ran the application again.

This also worked and told me that version 1.0.0.1 of MyLibrary had been loaded.

Hmm. This was not the errant behaviour the customer was reporting.

So I sent my sample application to the customer and she duly spotted that the syntax of the oldVersion attribute that I was using was subtly different to what she had.

The key point was that I had

<bindingRedirect oldVersion=”1.0.0.0-1.0.0.0″ newVersion=”1.0.0.1″ />

whereas she had something more like

<bindingRedirect oldVersion=”1.0-1.1″ newVersion=”1.0.0.1″ />

So I changed mine to say

<bindingRedirect oldVersion=”1.0-1.1″ newVersion=”1.0.0.1″ />

and

<requiredRuntime version=”v1.1.4322″ safemode=”true”/>

and it still worked.

I then changed it to use the 2.0 runtime:

<requiredRuntime version=”v2.0.50727″ safemode=”true”/>

Sure enough, it now failed.

A bit of digging showed that all our documentation for the syntax of version numbers in attributes like oldVersion is that they should include four parts and that each part can either be a number or a number range.  We don’t say the two digit syntax is valid though neither do we say it is invalid. Mind you it would be hard to document everything that is not valid. Usually you find out because something does not work. In this case it just works. The load in 2.0 just ignores the oldVersion attribute and so goes ahead and looks for version 1.0.0.0 of MyLibrary.dll that the EXE was compiled against. So if the old assembly is not available you get the dreaded “System.IO.FileNotFoundException – Could not load file or assembly..”

From asking around internally it seems this was unintentional that it could work that way in 1.1, which in turn led to an unintentional breaking change when migrating applications to 2.0 for anyone that had come to rely on the old syntax. Fortunately in this case the fix was just an easy edit in Notepad.

 

Here are the docs for the bindingRedirect element.

 

Bye for now

 

Doug


Comments (20)

  1. nadsure says:

    My project was running all fine. I needed to restart the machine and after that when i tried to run the project I got the error :

    Could not load file or assembly 'Aspose.Words' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

    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.IO.FileLoadException: Could not load file or assembly 'Aspose.Words' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

    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.  

    Assembly Load Trace: The following information can be helpful to determine why the assembly 'Aspose.Words' could not be loaded.

    How can I get rid of it? I cannot work untill this is resolved. My projectdoesnt load any pages because of this.

  2. Hello nadsure

    I've not come across your specific error myself but searching around the web for E_INVALIDARG and "Could not Load file or assembly" yields quite a few hits of people getting the same error for various reasons.

    It seems a pretty common solution is to clear out the shadow copy folder (you'll need to shutdown IIS and Visual Studio first):

    "C:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET"

    You just need to delete everything within that folder.

    Hope that helps

    Doug

  3. prithu says:

    Hi All,

    I am a new bee please help

    I keep getting this error "Could not load file or assembly 'service' or one of its dependencies.

    all the dll files are in place i also reset the iis services but nothing works

    Server Error in '/AnjumanLatest' Application.

    ——————————————————————————–

    Could not load file or assembly 'Anjuman_DAC' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    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.IO.FileLoadException: Could not load file or assembly 'Anjuman_DAC' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    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.  

    Assembly Load Trace: The following information can be helpful to determine why the assembly 'Anjuman_DAC' could not be loaded.

    WRN: Assembly binding logging is turned OFF.

    To enable assembly bind failure logging, set the registry value [HKLMSoftwareMicrosoftFusion!EnableLog] (DWORD) to 1.

    Note: There is some performance penalty associated with assembly bind failure logging.

    To turn this feature off, remove the registry value [HKLMSoftwareMicrosoftFusion!EnableLog].

    Stack Trace:

    [FileLoadException: Could not load file or assembly 'Anjuman_DAC' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]

      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 'Anjuman_DAC' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]

      System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +599

      System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +193

      System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +45

      System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +163

      System.Web.Compilation.BuildProvidersCompiler..ctor(VirtualPath configPath, Boolean supportLocalization, String outputAssemblyName) +56

      System.Web.Compilation.CodeDirectoryCompiler.GetCodeDirectoryAssembly(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories, Boolean isDirectoryAllowed) +533

      System.Web.Compilation.BuildManager.CompileCodeDirectory(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories) +161

      System.Web.Compilation.BuildManager.CompileCodeDirectories() +277

      System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +332

    [HttpException (0x80004005): Could not load file or assembly 'Anjuman_DAC' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]

      System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +60

      System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +432

      System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters) +609

    [HttpException (0x80004005): Could not load file or assembly 'Anjuman_DAC' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]

      System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +517

      System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +78

      System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +257

    ——————————————————————————–

    Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3614

  4. Hello Prithu

    As a first step I would suggest you try clearing the "temporary asp.net files" and see if that helps:

    blogs.msdn.com/…/clearing-out-temporary-asp-net-files.aspx

    Regards

    Doug

  5. Saurov Thakur says:

    1. Close VS

    2. Delete all temp asp .net files in "C:WINDOWSMicrosoft.NETFrameworkv4….Temporary ASP.NET"

    3. Reset IIS

    4. The dlls in your solution may be referenced from another folder and so they must be referred by BIN.

       Check if the BIN is excluded and Include it to the project. It should show all the references

    ..and  finally lemme know if this solves the prob…

  6. Chamara says:

    Hi,

    I'm trying to host my web site on production server but i'm getting the following error

    " Could not load file or assembly 'eWorld.UI, Version=2.0.6.2393, Culture=neutral, PublicKeyToken=24d65337282035f2' or one of its dependencies. The system cannot find the file specified."

    I have included all the assemblies in bin folder.Server uses .NET frame work 2.0 and i have used .NET frame work 3.5

    d3430264.u711.laknetwork.com/…/login.aspx

    above is the url to error page

  7. Are you sure that the assembly you have included in the BIN folder matches exacly the one mentioned above? It must match the version number, short name culture and public key token. Also it could be one of the assemblies it depends on that is missing. Look for FusLogVw in the SDK. You can use it to troubleshoot such issues

  8. azhar ali buttar says:

    Well, I am not a developer but I got to deploy a project which actually send fax using the fax services on windows 2008 server 64bit. the project was developed on a windows 32bit platform and I was setting all the information according to the given instruction but still I this annoying error and then I found the solution and that was to change the platform from anycpu to x86 and it worked. for a complete setting list please follow the link below:

    http://www.sizledcore.com/…/could-not-load-file-or-assembly

  9. Jamal Abdo says:

    If you can find the file and drop it in your bin folder, this can solve your problem. You can also directly reference the file

  10. vairamuthu says:

    Error 1 Unable to load referenced library 'C:WindowsApplication1WindowsApplication1objDebugInterop.ADOX.dll': The system cannot find the file specified.

    I could't solve this is problem pls help some to me and solve this issues

  11. Hello vairamuthu

    I suggest you ask in one of our MSDN forums. For example here was a thread on this topic a few years back. social.msdn.microsoft.com/…/d48b1f69-2557-4b66-93ad-c0d76a9b69e3

    Doug

  12. Ben Dent says:

    I use Visual Basic 2010 Express. It was working perefectly normally but now, if I try to run (debug) it i get a message saying:

    Error while trying to run project: Could not load file or assembly '"Rory's Application"' or one of its dependencies. The given assembly name or codebase was invalid (Exception from HRESULT: 0x80131047

    I tried to re-install VB but to no effect. How can I sort out this problem?

  13. Hello Ben  

    So your question reaches a wider audience I would recommend you post it to this forum:

    social.msdn.microsoft.com/…/home

    Regards

    Doug

  14. Could not load file or assembly 'SpreadsheetGear2012.Core says:

    Could not load file or assembly 'SpreadsheetGear2012.Core

  15. please help urgent says:

    Server Error in '/LinqChatVB' Application.

    Could not load file or assembly 'App_Code' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

    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 'App_Code' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

    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.

    Assembly Load Trace: The following information can be helpful to determine why the assembly 'App_Code' could not be loaded.

    === Pre-bind state information ===

    LOG: User = SUNDTHsowmiya.tr

    LOG: DisplayName = App_Code

    (Partial)

    LOG: Appbase = file:///D:/Dot Net Projects/LinqWebChatVB/LinqChatVB/

    LOG: Initial PrivatePath = D:Dot Net ProjectsLinqWebChatVBLinqChatVBbin

    Calling assembly : (Unknown).

    ===

    LOG: This bind starts in default load context.

    LOG: Using application configuration file: D:Dot Net ProjectsLinqWebChatVBLinqChatVBweb.config

    LOG: Using machine configuration file from C:WINDOWSMicrosoft.NETFrameworkv2.0.50727configmachine.config.

    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).

    LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/linqchatvb/c625c573/398dc67/App_Code.DLL.

    LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/linqchatvb/c625c573/398dc67/App_Code/App_Code.DLL.

    LOG: Attempting download of new URL file:///D:/Dot Net Projects/LinqWebChatVB/LinqChatVB/bin/App_Code.DLL.

    ERR: Failed to complete setup of assembly (hr = 0x8013101b). Probing terminated.

  16. Sorry, this is probably not the best place to seek urgent help. I would recommend you post in a forum such as MSDN forums or Stack Overflow or else open an assisted support case with Microsoft Developer Support

  17. ZaherElAli says:

    Good Day Gents,

    I'm getting a weird error out of nowhere, the error is, "Could not load Assembly "Core.Lib.Lightswitch.Client, version=1.0.0.0 Culture=neutral PublicKeyToken=null' or one of its dependencies. the system cannot find the specified file.

    Any help please?

    Regards,

  18. Hello, sounds like an assembly for a LightSwitch developed app. You might get more eyeballs on your problem over at the MSDN forum for Lightswitch social.msdn.microsoft.com/…/home

  19. ZaherElAli says:

    Thanks Doug, I'm still facing the same issue.

    seems like my app has been damaged 🙁

  20. Have you tried to reinstall the app?