加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

具有强名称的C#插件架构:误解

发布时间:2020-12-16 00:11:59 所属栏目:百科 来源:网络整理
导读:我有一个服务器可执行文件与Active Directory对话以检索用户信息.除了AD之外,这个exe还允许客户编写自己的插件来与自定义用户目录进行通信. 此可执行文件名称很强. 以下是真实的陈述: In order for a strongly named assembly to load another assembly,the
我有一个服务器可执行文件与Active Directory对话以检索用户信息.除了AD之外,这个exe还允许客户编写自己的插件来与自定义用户目录进行通信.

此可执行文件名称很强.

以下是真实的陈述:

In order for a strongly named
assembly to load another assembly,the
loaded assembly must also be signed
with the same key.

如果程序集没有强签名,则以下代码返回null,没有错误指示程序集未正确签名.请注意,如果我对程序集进行签名,我会得到一个IService实例.这让我相信加载的程序集必须是强签名的.

Assembly assembly = Assembly.LoadFrom(path);

foreach (Type t in assembly.GetTypes())
{
    if (t.GetInterface(typeof(IService).FullName) != null)
    {
      return (Activator.CreateInstance(t) as IService);
    }
}

那么,这是否意味着如果你有一个强签名的程序集和支持程序集插件,它们也必须签名 – 插件编写者必须用相同的密钥签名?这听起来不对.

最后,假设我有一个实现IService接口的程序集,但也引用了一个程序集,它引用了另一个程序集,每个程序集都用不同的密钥签名.我尝试加载时会发生什么?他们都应该用同一把钥匙签名吗?

解决方法

以下陈述是正确的:

In order for a strongly named assembly
to load another assembly,the loaded
assembly must also be signed with the same key .

从MSDN开始:

If the strong-named assembly then
references an assembly with a simple
name,which does not have these
benefits,you lose the benefits you
would derive from using a strong-named
assembly and revert to DLL conflicts.
Therefore,strong-named assemblies can
only reference other strong-named
assemblies.

编辑:D’哦!虽然我的答案是真的,正如P爸爸指出的那样,这是无关紧要的!

使用反射加载弱命名的程序集与引用程序集不同,并且不受同样的限制.

我使用以下程序集重新创建了您的代码(或至少近似):

> Interface.dll(已签名,包含IService)
> Loader.exe(已签名,一个采用路径的控制台应用程序,使用您的代码加载并返回它在该路径指定的程序集中找到的第一个IService,然后调用IService方法)
> Plugin.dll(未签名,包含IService实现)

接下来,我向Loaded.exe添加了一个Plugin.dll引用,并尝试访问其IService实现,该实现失败,如下所示:“程序集生成失败 – 引用程序集’插件’没有强名称.”

最后,我运行了控制台应用程序,并将其命名为名称不足的Plugin.dll,它运行得很好.

似乎还有其他事情正在发生. Scott Hanselman has blogged about the vagaries of
dynamic assembly loading on several occasions,他指向Suzanne Cook’s blog获取有关该主题的权威详细信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读