确实是的。如果您正在编写插件,则 app.config 文件对于重定向程序集毫无用处。该插件将首先查找机器配置用户计算机上的文件,然后查看*.config主程序的文件。
以下是我在为 SDL Trados 2017 编写插件时在插件场景中执行程序集绑定重定向的两步过程 -
第一步:在插件本身中使用 try-catch 语句来发现有关无法加载的程序集的信息.
就我而言,我怀疑创建 Google Cloud AutoML 客户端所需的少数程序集之一是罪魁祸首,因此我在插件首次尝试创建 Google Cloud AutoML 客户端的位置放置了一条 try-catch 语句:
try
{
client = AutoMlClient.Create();
}
catch (Exception err)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:/Desktop/log.txt", true))
{
file.WriteLine(err.ToString());
}
}
当我检查错误期间创建的“log.txt”文件时,我发现了以下信息:
System.IO.FileNotFoundException: Could not load file or assembly 'Google.Apis.Auth, Version=1.41.1.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab' or one of its dependencies. The system cannot find the file specified.
所以!很明显,在创建 AutoML 客户端的过程中,该插件试图查找 .Google.Apis.Auth 版本 1.41.1.0。但是,为了让我的插件正确编译,我必须使用 NuGet 安装 Google.Apis.Auth 版本 1.42.0.0。因此需要程序集绑定重定向。
第二步:添加与该特定程序集相关的事件处理程序,该事件处理程序将在加载之前更改其版本/公钥令牌信息。
在程序的一开始(初始化插件的主窗体窗口),我添加了以下代码:
public Main_form()
{
InitializeComponent();
Version V14200 = new Version("1.42.0.0");
RedirectAssembly("Google.Apis.Auth", V14200, "4b01fa6e34db77ab");
}
public static void RedirectAssembly(string assembly_name, Version targetVersion, string publicKeyToken)
{
ResolveEventHandler handler = null;
handler = (sender, args) => {
//gets the name of the assembly being requested by the plugin
var requestedAssembly = new AssemblyName(args.Name);
//if it is not the assembly we are trying to redirect, return null
if (requestedAssembly.Name != assembly_name)
return null;
//if it IS the assembly we are trying to redirect, change it's version and public key token information
requestedAssembly.Version = targetVersion;
requestedAssembly.SetPublicKeyToken(new AssemblyName("x, PublicKeyToken=" + publicKeyToken).GetPublicKeyToken());
requestedAssembly.CultureInfo = CultureInfo.InvariantCulture;
//finally, load the assembly
return Assembly.Load(requestedAssembly);
};
AppDomain.CurrentDomain.AssemblyResolve += handler;
}
因此基本上,您必须从插件(使用 try-catch 语句)获取有关哪个程序集无法加载的信息。然后,您必须添加一个事件处理程序,该事件处理程序将在相关程序集开始加载时生效。
就我而言,我知道 Google.Apis.Auth 是问题所在 - 该插件想要加载版本 1.41.1.0,但我的插件包含版本 1.42.0.0。当插件开始查找 Google.Apis.Auth (1.41.1.0) 时,事件处理程序介入并更改版本号,因此插件加载版本 1.42.0.0。
由于从未接受过任何计算机科学或编程方面的正式培训,我不知道这个解决方案有多强大/值得推荐,但它对我有用。