2

I have been trying to load a private assembly which is located in a subdirectory under the application base directory. I have an assembly named Sparrow.dll which is located under plugins directory (which is under application base dir also). Whenever I call Assembly.Load("Sparrow") I get a System.IO.FileNotFoundException.

I used app.exe.config with tag and it worked with a strong named version of the same assembly with the line below;

Assembly assem = Assembly.Load("Sparrow");

However, it does not work when I changed the assembly in to a weak assembly.

The content of the config file is below;

<configuration>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly name="Sparrow, Culture=neutral, PublicKeyToken=xxxxxx">
            <codeBase version="1.0.1.1" href="plugins/Sparrow.dll" />
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

I read many things, but I am not sure whether using tag for locating weak assemblies is a good practice or not.

Jean Hominal
  • 16,518
  • 5
  • 56
  • 90
Deniz
  • 858
  • 10
  • 31

1 Answers1

2

You can use the probing element for that purpose:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="plugins" />
    </assemblyBinding>
  </runtime>
</configuration>

That element means that the plugins subfolder will be searched for assemblies. Note however, that only directories that are on a descendent path of the application directory can be specified in that way.

The configuration file in your question has a mistake. According to documentation, the XML configuration should look like:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <dependentAssembly>
       <assemblyIdentity name="Sparrow"
                          publicKeyToken="null"
                          culture="neutral" />
       <codeBase version="1.0.1.1" href="plugins/Sparrow.dll" />
     </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

However, I think that using the probing element would be a better choice in your case.

Jean Hominal
  • 16,518
  • 5
  • 56
  • 90
  • That's exactly how I solved the problem. For weak and private assemblies it is very easy and also works well as far as I see. However, there is lots of information about assemblies which causes the confusion. As far as I understood from the MSDN, using for weak assemblies is possible and feasible. – Deniz Oct 23 '14 at 15:12
  • @Deniz: Did you use a `probing` element with a `privatePath` attribute? Because I do not see that in your question - you used a different element. Also, your current solution is not well-adapted to a system with plugins, as you would need to modify the configuration file for every plugin you would add. – Jean Hominal Oct 23 '14 at 15:21
  • Not in this question, but I tested it on my computer. You are right, if I use different folders for different plugins then I should add each of them. May be putting every assembly in to a single folder (reserved for plugin assemblies) simplifies the problem. I am open to different solutions of course. – Deniz Oct 23 '14 at 15:27
  • @Deniz: After re-reading your question, I have seen that there is a mistake in the `` element. Does that fix anything for you? – Jean Hominal Oct 23 '14 at 15:43
  • You are right. But still not working. By the way, it is a little bit weird for .NET to allow such kind of errors, is't it? – Deniz Oct 23 '14 at 15:56
  • @Deniz: Please look at [this blog post](http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx) from Scott Hanselmann to debug the issue - I do not have enough information to help you. – Jean Hominal Oct 23 '14 at 16:00