AppDomain.CurrentDomain.AssemblyResolve问题

时间:2015-09-30 12:04:19

标签: c# wpf xaml exception assemblyresolve

我有单个应用程序,动态加载以下三个解决方案

 - SolutionA which references
                 X.dll version 3
                 Z.dll version 1 which references
                                       X.dll version 1

 - SolutionB which references
                 X.dll version 2

 - SolutionC which references
                 X.dll version 1

X.dll包含一些XAML(取决于我更改文本,背景和前景的DLL版本):

<UserControl x:Class="X.MainWindow"
             Background="DodgerBlue">
    <UserControl.Content>
        <Label Foreground="Black" Content="version 1" />
    </UserControl.Content>
</UserControl>

Z.dll还包含一些使用X.dll的XAML:

<UserControl x:Class="Z.ZMainWindow">
    <StackPanel>
        <X:MainWindow />
    </StackPanel>
</UserControl>

SolutionA显示X版本3和Z版本1(显示X版本1)

SolutionB显示X版本2

SolutionC显示X版本1

我想要的是 总是使用更高版本的程序集 ,这意味着SolutionA,SolutionB和SolutionC必须始终使用X.dll 版本3

以下是我用于AssemblyResolve的代码:

private static Assembly OnAssemblyResolveWithDllPaths(object sender, ResolveEventArgs args)
{
    try
    {
        AssemblyName parsedName = new AssemblyName(args.Name);
        Version currentVersion = parsedName.Version;
        string currentName = parsedName.Name;
        string currentCulture = parsedName.CultureName;
        byte[] currentPublicKeyToken = parsedName.GetPublicKeyToken();
        string assemblyPath = string.Empty;

        if (loadedAssemblies.FirstOrDefault(a => a == args.Name) != null)
        {
            // Assembly has already been loaded
            return null;
        }

        List<string> possibleAssembliesPaths = m_dllPaths.Where(d => d.Contains(parsedName.Name)).ToList();

        if (possibleAssembliesPaths != null && possibleAssembliesPaths.Count > 0)
        {
            if (possibleAssembliesPaths.Count == 1)
            {
                assemblyPath = possibleAssembliesPaths[0];
            }
            else
            {
                foreach (string possibleAssembly in possibleAssembliesPaths)
                {
                    if (File.Exists(possibleAssembly))
                    {
                        AssemblyName possibleAssemblyName = AssemblyName.GetAssemblyName(possibleAssembly);
                        Version possibleVersion = possibleAssemblyName.Version;
                        string possibleName = possibleAssemblyName.Name;
                        string possibleCulture = possibleAssemblyName.CultureName;
                        byte[] possiblePublicKeyToken = possibleAssemblyName.GetPublicKeyToken();

                        if (currentName == possibleName 
                            && currentCulture == possibleCulture 
                            && currentPublicKeyToken.SequenceEqual(possiblePublicKeyToken) 
                            && currentVersion < possibleVersion)
                        {
                            assemblyPath = possibleAssembly;
                            currentVersion = possibleAssemblyName.Version;
                            currentName = possibleAssemblyName.Name;
                            currentCulture = possibleAssemblyName.CultureName;
                            currentPublicKeyToken = possibleAssemblyName.GetPublicKeyToken();
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(assemblyPath))
            {
                loadedAssemblies.Add(args.Name);
                return Assembly.LoadFrom(assemblyPath);
            }
        }
    }
    catch
    {
        return null;
    }

    return null;
}

一切都很完美,直到名字我的控件

即。 (在Z.dll中):<Label Foreground="Black" Content="version 1" />变为<Label Foreground="Black" Content="version 1" x:Name="LabelV1" />

和(在X.dll中):<X:MainWindow x:Name="XMainWindow" />变为<X:MainWindow x:Name="XMainWindow" />

此时,SolutionA有一个异常,并且在SolutionC之前加载SolutionA时不会显示(SolutionB永远不会出现问题,只要在SolutionA之前加载SolutionC,一切都运行正常)。

我看到,当我附加到AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad时,XAML会自动加载他们需要的程序集:

SolutionC's XAML loads X in version 1
SolutionA's XAML loads X in version 3
SolutionA's XAML loads Z in version 1

然后我在xaml.cs中看到以下异常:

An exception of type 'System.Exception' occurred in PresentationFramework.dll but was not handled in user code
Additional information: The component 'X.MainWindow' does not have a resource identified by the URI '/X;component/mainwindow.xaml'.

以及xaml中的以下内容:

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type 'X.MainWindow' that matches the specified binding constraints threw an exception.' Line number '10' and line position '10'.

您是否有线索以避免此问题?

0 个答案:

没有答案