装载装配问题

时间:2012-07-19 04:16:27

标签: c#

我正在为应用程序编写加载项。 AddIn是一个Dll,它位于特定文件夹%APPDATA& / Application / AddIns / MyAddIn

所以,我有一个包含多个项目的解决方案,而MyAddInFolder包含以下文件:

IVC.Common.dll 
IVC.MaterialCatalogEditorAddIn.dll
IVC.MaterialCatalogEditor.dll

IVC.MaterialCatalogEditor.dll 中的应用程序调用方法和 IVC.MaterialCatalogEditor.dll 引用的 IVC.MaterialCatalogEditorAddIn.dll 以及 IVC.Common.dll 引用的 IVC.MaterialCatalogEditor.dll

IVC.MaterialCatalogEditor.dll 中有一个WPF froms。 xaml文件引用了 IVC.Common.dll

当我尝试从应用程序执行AddIn命令时,我收到异常“无法加载程序集 IVC.Common.dll

我使用了FusionLogViewer并获得了以下消息日志

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.

这一切都好。 IVC.MaterialCatalogEditorAddIn.dll 在AddIn文件夹中调用* IVC.MaterialCatalogEditor.dll *

但下一个日志:

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.

PresebtationFramework程序集尝试调用 IVC.Common.dll 并尝试仅从应用程序文件夹加载。

为什么一个dll尝试从AddIn文件夹加载,但是另一个dll只是尝试从应用程序文件夹加载?以及如何解决我的问题。

所有项目都有任何CPU平台目标。该应用程序是x64。

当我从测试可执行应用程序加载 IVC.MaterialCatalogEditor.dll 时,每件事都可以正常工作。

EDITED 一些代码:

IVC.MaterialCatalogEditorAddIn.dll

中的主要代码
[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;
        }
    }

MaterialsWindow类和MaterialsViewModel类位于 IVC.MaterialCatalogEditor.dll

debbuger在 IVC.MaterialCatalogEditor.dll 中的xaml部分显示错误:

<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">
<DataTemplate
            DataType="{x:Type Common:ResourceWithCharge}">
<!-- Some data template -->
</DataTemplate>
</ResourceDictionary>

已编辑2 我编写的测试解决方案与我工作的解决方案中的结构相同。 当我从测试应用程序启动它时,它没有任何错误。但是,如果我从AddIn执行它失败,“无法加载程序集Common.dll”

Test Solution

2 个答案:

答案 0 :(得分:1)

虽然装配探测失败了,但还有几种方法可以解决:

  1. 使用app.config文件更改程序集绑定
  2. 手柄组装 通过附加到当前应用程序的AssemblyResolve来加载失败 域和使用c#代码加载必要的程序集。

答案 1 :(得分:0)

这是因为在加载编译的XAML(BAML)时,WPF在默认上下文中使用Assembly.Load()加载了IVC.Common.dll。

您应该尝试通过以下方式编辑Revit.exe.config:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>  
     <assemblyIdentity name="IVC.Common" />  
       <codeBase href="<Not sure how you can put %APPDATA% here>\Application\AddIns\MyAddIn\IVC.Common.dll"/>  
     </dependentAssembly>  
  </assemblyBinding>
</runtime>