Dela via


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

  • Anonymous
    January 24, 2011
    The comment has been removed

  • Anonymous
    January 26, 2011
    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

  • Anonymous
    February 02, 2011
    The comment has been removed

  • Anonymous
    February 02, 2011
    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

  • Anonymous
    March 24, 2011

  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...
  • Anonymous
    April 09, 2011
    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

  • Anonymous
    April 10, 2011
    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

  • Anonymous
    October 04, 2011
    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: www.sizledcore.com/.../could-not-load-file-or-assembly

  • Anonymous
    April 05, 2013
    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

  • Anonymous
    June 15, 2013
    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

  • Anonymous
    June 16, 2013
    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

  • Anonymous
    June 26, 2013
    The comment has been removed

  • Anonymous
    June 30, 2013
    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

  • Anonymous
    November 23, 2014
    Could not load file or assembly 'SpreadsheetGear2012.Core

  • Anonymous
    November 24, 2014
    The comment has been removed

  • Anonymous
    November 24, 2014
    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

  • Anonymous
    May 14, 2015
    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,

  • Anonymous
    May 14, 2015
    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

  • Anonymous
    May 15, 2015
    Thanks Doug, I'm still facing the same issue. seems like my app has been damaged :(

  • Anonymous
    May 18, 2015
    Have you tried to reinstall the app?

  • Anonymous
    February 16, 2018
    Hey great post! I hope it's ok that I shared it on my FB, if not, no worries just let me know and I'll delete it. Either way keep up the great work.

  • Anonymous
    February 21, 2018
    I really like reading through a post that will make men and women think. Also, many thanks for permitting me to comment!

  • Anonymous
    February 22, 2018
    I do not know if it's just me or if everyone else encountering issues with your site. It seems like some of the written text within your content are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them as well?This may be a problem with my web browser because I've had this happen previously.Thanks