C#插件系统二进制兼容性问题
我在.NET 中实现了一个插件系统。基础库实现了暴露给插件的基本类和接口,插件库链接基础库以使用暴露的类和接口。
我面临的问题是,基础库的(简单)重新编译(有或没有修改)导致插件无法加载,给出异常消息:
"Could not load file or assembly 'BaseLibrary, Version=0.0.1.68, Culture=neutral, PublicKeyToken=7b445b12e635292c' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"
这个问题是通过一次编译所有基础库来解决的,并且插件库,但这在开发过程中不太舒服,因为我在这个阶段经常修改基础库。
是否有任何方法可以“放松”二进制匹配?
基础库程序集信息(下面引用)是否可能是问题的原因?
[assembly: AssemblyVersion("0.0.1.*")]
我忘了提及程序集已签名。
使用以下例程加载程序集
Assembly hLibrary = Assembly.LoadFile(pPath);
Type plugImageCodecFactoryType = hLibrary.GetType("Derm.ImageCodecPluginFactory", true, false);
object plugImageCodecFactory = Activator.CreateInstance(plugImageCodecFactoryType);
object plugInstance;
MethodInfo plugFactoryCreate = plugImageCodecFactoryType.GetMethod("CreatePlugin", BindingFlags.Instance|BindingFlags.Public);
plugInstance = plugFactoryCreate.Invoke(plugImageCodecFactory, null);
if (plugInstance is IImageCodecPlugin)
RegisterPlugin((IImageCodecPlugin)plugInstance);
I've implemented a plugin system in .NET. The base library implements basic classes and interfaces exposed to plugins, the plugin libraries links the base library for using exposed classes and interfaces.
The problem I'm facing is that a (simple) recompilation of the base library (with or without modifications) cause the plugins not able to being loaded, giving the exception message:
"Could not load file or assembly 'BaseLibrary, Version=0.0.1.68, Culture=neutral, PublicKeyToken=7b445b12e635292c' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"
This problem is solved by compiling all at once the base library and the plugin libraries, but this is not very comfortable during development, since I modify base library quite frequently in this phase.
If there any method to "relax" binary matching?
Is it possible that the base library assembly information (quoted below) could be the casue of the problem?
[assembly: AssemblyVersion("0.0.1.*")]
I forgot to mention that assemblies are signed.
Assemblies are loaded using the following routine
Assembly hLibrary = Assembly.LoadFile(pPath);
Type plugImageCodecFactoryType = hLibrary.GetType("Derm.ImageCodecPluginFactory", true, false);
object plugImageCodecFactory = Activator.CreateInstance(plugImageCodecFactoryType);
object plugInstance;
MethodInfo plugFactoryCreate = plugImageCodecFactoryType.GetMethod("CreatePlugin", BindingFlags.Instance|BindingFlags.Public);
plugInstance = plugFactoryCreate.Invoke(plugImageCodecFactory, null);
if (plugInstance is IImageCodecPlugin)
RegisterPlugin((IImageCodecPlugin)plugInstance);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
阅读这些问题和答案,以更深入地讨论 AssemblyVersion 与 AssemblyFileVersion 的使用。
引用 DLL 时忽略内部版本号
AssemblyVersion 和 AssemblyFileVersion 之间的差异
版本的问题是,只有当您对该程序集引入潜在的破坏性更改(这将迫使依赖项采用新版本)时,您才应该更改 AssemblyVersion。
对于较小的更改,您可以使用 AssemblyFileVersion 来标记差异。
因此,我将使用静态程序集版本进行开发,当您获得想要管理的稳定版本并手动管理未来版本增量时,增加它。
Read these questions and answers for a more thorough discussion on the use of AssemblyVersion vs AssemblyFileVersion.
Ignoring build numbers when referencing DLLs
Differences between AssemblyVersion and AssemblyFileVersion
The short version is that you should only change AssemblyVersion when you introduce a potentially breaking change on that assembly that will force dependants to take a new version.
For minor changes you can use AssemblyFileVersion to mark differences.
So I would use a static assembly version for development, increment it when you get to a stable release that you want to manage going forward and manage future version increments manually.
假设您正在使用 Visual Studio,请查看作为插件的项目之一中的引用。当您查看对基础库的引用的属性时,
SpecificVersion
标志设置为什么?尝试将其设置为False
,看看是否会产生影响。请注意,如果您的项目使用项目引用而不是对已编译 DLL 的直接引用,则此标志将不会出现。在这种情况下,您要么需要像您尝试过的那样在基础库上使用固定修订版号,要么应该改用对 DLL 的直接引用,以便可以更改
SpecificVersion
属性。Assuming you are using Visual Studio look at the references in one of the projects that is a plugin. When you look at the properties for the reference to your base library what is the
SpecificVersion
flag set to? Try setting it toFalse
and see if that makes a difference.Note that if your project is using a project reference rather than a direct reference to the compiled DLL this flag won't be present. In this case you either need to use a fixed revision number on the base library like you've tried or you should switch to using a direct reference to the DLL instead so you can alter the
SpecificVersion
property.对我来说,这里与插件无关,因为编译器错误谈论的是基础库,并且考虑到您实现了插件基础架构,插件必须完全解耦并延迟加载。所以即使存在插件加载问题,你也会在运行时遇到,而不是编译时。
所以对我来说,原因是你的汇编版本。
问候。
To me here is nothing related to plugins as compiler error talks about base library, and considering that you implemented plugin base architecture, plugins have to be completely decopled and loaded lazy. So even if there is a plugin loading problem, you will meet at runtime and not compile time.
So for me the cause is your assembly version.
Regards.