0

My problem seems to be very simple, but i can't find a solution.

I have a host app, which hosts usercontrols from third-party libraries, which I call plugins. Each plugin has it's folder, where all plugin files are located and I use LoadFrom to load main assembly. My plugin must implement interface, which looks like this:

public interface IPlugin : IServiceProvider, IDisposable
{
    FrameworkElement CreateControl();
}

and I load concrete implementation of this interface from "main" plugin library. The problem is that when I try to invoke method CreateControl() I catch an exception

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Could not load file or assembly 'Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4' or one of its dependencies. Could not find specified file.

which means that dependencies were not loaded via loadfrom context. In FusionLog i can see, that reflection searched everywhere but plugin folder, which looks very strange for me. How can i deal with this problem?

Code, that loads "main" type looks like this:

AssemblyName assemblyName = AssemblyName.GetAssemblyName(fileToLoad);
var assembly = Assembly.LoadFrom(fileToLoad);
var type = assembly.GetType(typeName);

if (type == null) 
    throw new InvalidOperationException("Could not find type " + typeName + " in assembly " + fileToLoad);

var defaultConstructor = type.GetConstructor(new Type[0]);
if (defaultConstructor == null)
{
    var message = String.Format("Cannot create an instance of {0}. Either a public default constructor, or a public constructor taking IWpfHost must be defined", typeName);
    throw new InvalidOperationException(message);
}

return defaultConstructor.Invoke(null);

Here is InnerException FusionLog:

    === Pre-bind state information ===
LOG: DisplayName = Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4 | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\voskresenskiy\Documents\Visual Studio 2013\Projects\UIContainerIT\bin\Debug\Liga Studio.vshost.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: 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:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/lib/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/lib/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/lib/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/lib/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.EXE.

As you can see from log reflection searches lib folder (i already have specified probing attribute in app.config like this: <probing privatePath="lib" />) and doesn't search in file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/plugin/PluginName/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.EXE

I'll be gratefull for any help!

Alex Voskresenskiy
  • 2,143
  • 2
  • 20
  • 29

1 Answers1

2

Use assembly binding - probing options

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

If you have a directory for each plugin, then you can add all directories separated by ; like plugin1;plugin2;...pluginX; in the probing element.

pepo
  • 8,644
  • 2
  • 27
  • 42
  • Thanks for your reply! When i added plugins folder to probing reflection searches only plugins folder, not subfolders. My plugins will be added after delpoy, so i can't add all subfolders. Can i somehow setup this programmatically? – Alex Voskresenskiy Apr 08 '14 at 13:01
  • [Here](http://stackoverflow.com/questions/10345240/c-sharp-set-probing-privatepath-without-app-config) is a similar question to what you want to do. – pepo Apr 08 '14 at 13:09
  • Okay, i got the answer. We'll, with your help i got this working, thank you much! – Alex Voskresenskiy Apr 08 '14 at 13:13