Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly 'XXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies

Tags:

c#

I have been trying to debug this Error:

System.IO.FileNotFoundException: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. File name: 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

I see a lot of solutions out there and try them all.

I am looking at Fusion Viewer and I am getting the following message:

LOG: This bind starts in default load context. LOG: Using application configuration file: I:\Projects\ACE Explorer\AceV_1Explorer\AceExplorer\AceExplorer\bin\Debug\AceExplorer.exe.config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Post-policy reference: ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null LOG: GAC Lookup was unsuccessful. LOG: Attempting download of new URL file:///I:/Projects/ACE Explorer/AceV_1Explorer/AceExplorer/AceExplorer/bin/Debug/ClassLibrary1.DLL. LOG: Attempting download of new URL file:///I:/Projects/ACE Explorer/AceV_1Explorer/AceExplorer/AceExplorer/bin/Debug/ClassLibrary1/ClassLibrary1.DLL. LOG: Attempting download of new URL file:///I:/Projects/ACE Explorer/AceV_1Explorer/AceExplorer/AceExplorer/bin/Debug/ClassLibrary1.EXE. LOG: Attempting download of new URL file:///I:/Projects/ACE Explorer/AceV_1Explorer/AceExplorer/AceExplorer/bin/Debug/ClassLibrary1/ClassLibrary1.EXE. LOG: All probing URLs attempted and failed.

I see the file in the directory and the Target framework is 4.0 for all using x86

Any ideas of finding out what is broken

Updated Sorry for the confusion. In the log it was PublicKeyToken=null. I was trying everything.

UPDATE I added some code in the main program that will resolve assemblies using the ResolveEventHandler. It didn't work at first. But when I had the program load those external assemblies (LoadReferences()) in the start of the program it do work. Weird since the path was exactly the same when I load them in the beginning LoadReferences() and then when the method enters a reference to a custom assemble which triggers the CurrentDomain_AssemblyResolve. The blank path resolves to the assemblypath. As you can see, in the LoadReferences(), I am adding EntityFramework which will cause the same issue, even though I am using Nuget.

In Main() I added:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
LoadReferences();



private static void LoadReferences()
        {
            Assembly  objExecutingAssemblies;
            objExecutingAssemblies = Assembly.GetExecutingAssembly();
            AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();
            Assembly asm;
            foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
            {
                if (strAssmbName.FullName.ToLower().Contains("[company].") || strAssmbName.FullName.ToLower().Contains("entityframework"))
                    asm = Assembly.LoadFrom(Path.Combine("", strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(","))+ ".dll"));


            }
            // these were also posing an issue
            asm = Assembly.LoadFrom(Path.Combine("", "EntityFramework.dll"));
            asm = Assembly.LoadFrom(Path.Combine("", "EntityFramework.SqlServer.dll"));


        }
        static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string assembliesDir = "";
            try
            {
                string dll = (args.Name.IndexOf(",") < 0 ? args.Name + ".dll" : args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll");
                Assembly asm = Assembly.LoadFrom(Path.Combine(assembliesDir, dll));
                return asm;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return null;
        }

Also. Not sure if this makes a difference but it is a clickonce with full trust.

Also our desktops are pretty locked down.

Also platform is set to x86 for all projects.

like image 920
H20rider Avatar asked May 06 '16 21:05

H20rider


People also ask

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.

Why is PublicKeyToken null?

PublicKeyToken = null tells you that the CLR is looking for the unsigned assembly. Since you signed them, that's not going to work well and this kaboom is expected. You will have to rebuild the program so it uses the updated signed assembly and embeds the non-null PublicKeyToken into the manifest.

Could not load file or assembly DLL file?

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 cryptography or one of its dependencies An 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.


1 Answers

file:///I:/Projects/ACE Explorer/.../bin/Debug/ClassLibrary1.DLL.

This question requires psychic debugging. Fusion is asked to resolve a path on the I: drive. That is often a mapped drive letter to a share on a file server. And often troublesome like this. Given the excessive focus on security, ACE is liable to be the acronym for Access Control Entry. Which makes it likely that the program uses impersonation to "explore" access rights. Which makes it likely that Fusion can't figure out what the I: drive might refer to, drive mappings are a per-user setting.

Works okay early in the program because it still runs with the default user token. And works okay once the program is deployed since it now has a stable location that doesn't depend on a drive letter mapping.

Not sure what to recommend, I am a charter member of the Geneva Convention on Programmer's Rights. Which demands that programmers can create and test programs on their local drive and install the kind of debugging tools they need to find out why their program doesn't work. This is a problem that your IT staff needs to solve. If that gives you control over your dev machine back then you're ahead. A likely outcome given the kind of hits that Google returns for a ".net debug project on mapped drive" query.

like image 77
Hans Passant Avatar answered Dec 25 '22 02:12

Hans Passant