3

Hi I know this has been asked but it did not get an answer. I have a problem when I want to use a dll that is installed on C:\Program files (x86)\Dummu_API.dll

When I run my app it throws exception:

Could not load file or assembly 'Dummy_API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

My project have the reference as Copy Local = false and specific version = false. My project also is a x86 platform target.

I don´t know why my app don't find the assembly. Any Idea what it is happening?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Yoiku
  • 882
  • 2
  • 13
  • 25
  • 3
    If it's not in the same directory as the executable and not in the GAC how do you expect your application to find it? – Lloyd Feb 19 '14 at 22:33
  • Basically what you are saying is that I can not reference a Dll from (for example) my documents unless I install it into GAC? How do I install the Dummy_API in the Global Assembly Cache? – Yoiku Feb 19 '14 at 22:42
  • 1
    It would easier to just deploy Dummy_API.dll with your application. To install the DLL in the GAC would require you to touch every machine with a command line. – Black Frog Feb 20 '14 at 00:29

3 Answers3

5

Option 1

You can tell your application that you will resolve the Assemblies yourselves if not found in references. To do that:

In your applications main method attach assembly resolver:

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

Make sure you are not using the dynamically resolved assembly in your main method.

Your resolver method code (This is where you load an assembly - look at ResolveEventArgs I have just hard coded one but you can resolve various assemblies from different locations here)

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    return Assembly.LoadFile(@"C:\temp\ClassLibrary1.dll");
}

Option 2

Add this to your app.config (Only works for application base directory's sub folders)

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <probing privatePath="PathToMyAssemblies\" />
  </assemblyBinding>
</runtime>
lahsrah
  • 9,013
  • 5
  • 37
  • 67
  • it did not work. This sounds like it could work, there is another way to indicate my program where to search the assemblies? – Yoiku Feb 19 '14 at 22:46
  • 2
    Sorry - i just read this only works for sub directories of app base directory – lahsrah Feb 19 '14 at 22:48
  • Well thank you this can be helpful because I can just "copy local" on other directory so I can hide the numerous dlls that I need :) is not a solution but it is a work arround – Yoiku Feb 19 '14 at 22:50
1

So the file is not in the GAC, and there is not a local copy. You've eliminated the only two places it could be loading from - I don't know why you are expecting this to work.

You need to set Copy Local=true or install Dummy_API.dll into the Global Assembly Cache on the machine that will be running your code. One or the other.

Joel Mueller
  • 28,324
  • 9
  • 63
  • 88
  • How do I install the Dummy_API in the Global Assembly Cache? But basically what you are saying is that I can not reference a Dll from (for example) my documents unless I install it into GAC? – Yoiku Feb 19 '14 at 22:39
  • @user2510947 by default your executable will probe for assemblies from the GAC, from the local directory (e.g. CopyLocal=true), or from probing paths as configured in lahsrah's answer. It won't search the machine for Dummy_API.dll in any other folders. – Jimmy Feb 20 '14 at 00:03
0

I don`t know if you chose the right project type, as for me, I found me selecting the dot net core type(console app) project at the very beginning, then I shifted the project to dot net framework(console app), and it works for me.

There`s another quick way to check if you have the same issue as I do:

  1. After you create item app.config and compiled it, you found .dll.config but not .exe.config
  2. Reference tab replaced by dependencys tab
  3. Project property framework selection can only be core x.x or net 5.0 but not framework 3.0/4.0/4.5 etc.

Just a hint or tip, hope helped!