Problems with load assembly
<p>I'm writing Add-In for the application.
AddIn is a Dll and it is located in the specific folder %APPDATA&amp;/Application/AddIns/MyAddIn</p>

<p>So, I have a solution with several projects and the MyAddInFolder contains following files:</p>

<pre><code>IVC.Common.dll 
IVC.MaterialCatalogEditorAddIn.dll
IVC.MaterialCatalogEditor.dll
</code></pre>

<p>The application calls method located in the <strong>IVC.MaterialCatalogEditorAddIn.dll</strong> and <strong>IVC.MaterialCatalogEditorAddIn.dll</strong> referenced to the <strong>IVC.MaterialCatalogEditor.dll</strong> and the <strong>IVC.MaterialCatalogEditor.dll</strong> referenced to the <strong>IVC.Common.dll</strong> </p>

<p>There is a WPF froms in the <strong>IVC.MaterialCatalogEditor.dll</strong>. And on of the xaml file is referenced to the <strong>IVC.Common.dll</strong> </p>

<p>When I try to execute AddIn command from the application I get an exception "Could not load assembly <strong>IVC.Common.dll</strong>"</p>

<p>I used FusionLogViewer and I got the following message logs</p>

<pre><code>Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Program Files\Autodesk\Revit Structure 2012\Program\Revit.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = SK\ChekalinVV
LOG: DisplayName = IVC.MaterialCatalogEditor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files/Autodesk/Revit Structure 2012/Program/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = Revit.exe
Calling assembly : IVC.MaterialCatalogEditorAddIn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Program Files\Autodesk\Revit Structure 2012\Program\Revit.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:/Program Files/Autodesk/Revit Structure 2012/Program/IVC.MaterialCatalogEditor.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit Structure 2012/Program/IVC.MaterialCatalogEditor/IVC.MaterialCatalogEditor.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit Structure 2012/Program/IVC.MaterialCatalogEditor.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit Structure 2012/Program/IVC.MaterialCatalogEditor/IVC.MaterialCatalogEditor.EXE.
LOG: Attempting download of new URL file:///C:/Users/ChekalinVV/AppData/Roaming/Autodesk/Revit/Addins/2012/IVC/FamilyManager/IVC.MaterialCatalogEditor.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\ChekalinVV\AppData\Roaming\Autodesk\Revit\Addins\2012\IVC\FamilyManager\IVC.MaterialCatalogEditor.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: IVC.MaterialCatalogEditor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
LOG: Where-ref bind Codebase does not match what is found in default context. Keep the result in LoadFrom context.
LOG: Binding succeeds. Returns assembly from C:\Users\ChekalinVV\AppData\Roaming\Autodesk\Revit\Addins\2012\IVC\FamilyManager\IVC.MaterialCatalogEditor.dll.
LOG: Assembly is loaded in LoadFrom load context.
</code></pre>

<p>Here is everything ok. The <strong>IVC.MaterialCatalogEditorAddIn.dll</strong> calls *<em>IVC.MaterialCatalogEditor.dll</em>*in the AddIn folder</p>

<p>But the next log:</p>

<pre><code>Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Program Files\Autodesk\Revit Structure 2012\Program\Revit.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = SK\ChekalinVV
LOG: DisplayName = IVC.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files/Autodesk/Revit Structure 2012/Program/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = Revit.exe
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:\Program Files\Autodesk\Revit Structure 2012\Program\Revit.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:/Program Files/Autodesk/Revit Structure 2012/Program/IVC.Common.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit Structure 2012/Program/IVC.Common/IVC.Common.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit Structure 2012/Program/IVC.Common.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Revit Structure 2012/Program/IVC.Common/IVC.Common.EXE.
LOG: All probing URLs attempted and failed.
</code></pre>

<p>The PresebtationFramework assembly try called the <strong>IVC.Common.dll</strong> and try loaded only from application folder.</p>

<p>Why the one dll is trying loaded from the AddIn folder, but another dll is trying loaded from application folder only? And how to solve my problem.</p>

<p>All project has Any CPU Platform target. The application is x64.</p>

<p>When I load <strong>IVC.MaterialCatalogEditor.dll</strong> from the test executable application everythings work fine.</p>

<p><strong>EDITED</strong>
Some code:</p>

<p>The main code in the <strong>IVC.MaterialCatalogEditorAddIn.dll</strong></p>

<pre><code>[Transaction(TransactionMode.ReadOnly)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {            

            MaterialsWindow materialsWindow =
                new MaterialsWindow();
            materialsWindow.DataContext = 
                new MaterialsViewModel();

            materialsWindow.ShowDialog();

            return Result.Succeeded;
        }
    }
</code></pre>

<p>The MaterialsWindow class and the MaterialsViewModel class are located in the <strong>IVC.MaterialCatalogEditor.dll</strong></p>

<p>The debbuger show error on this part of xaml in the <strong>IVC.MaterialCatalogEditor.dll</strong>:</p>

<pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                    
                    xmlns:ViewModel="clr-namespace:IVC.MaterialCatalogEditor.ViewModel"
                    xmlns:Common="clr-namespace:IVC.Common;assembly=IVC.Common"&gt;
&lt;DataTemplate
            DataType="{x:Type Common:ResourceWithCharge}"&gt;
&lt;!-- Some data template --&gt;
&lt;/DataTemplate&gt;
&lt;/ResourceDictionary&gt;
</code></pre>

<p><strong>EDITED 2</strong>
I've written the test solution with the same structure as in the my working solution.
It is works without any errors when I launch it from the test application. But if I execute it from the AddIn it fails with "Could not load assembly Common.dll"</p>

<p><a href="https://dl.dropbox.com/u/62538318/Revit/TestAssemblyResolveProject.zip" rel="nofollow">Test Solution</a></p>
