复杂的API问题,动态调用组件组件、复杂、动态、问题

2023-09-06 08:38:09 作者:何必自编自演自欺欺人

我有,我想知道如果任何人在这里可以给我一些指导一个有趣的挑战。

I have an interesting challenge that I'm wondering if anyone here can give me some direction.

我在写一个.net Windows窗体上运行的网络上,并使用SQL Server来保存和提取数据的应用程序。

I'm writing a .Net windows forms application that runs on a network and uses an SQL Server to save and pull data.

我想提供一个迷你插件的API,开发人员可以建立自己的组件和实现特定接口(IDataManipulate)。这些组件然后可以使用我的应用程序调用的接口函数,做一些事情。

I want to offer a mini "plugin" API, where developers can build their own assemblies and implement a specific interface (IDataManipulate). These assemblies then can be used by my application to call the interface functions and do something.

我可以用我的API创建组件,将文件复制到文件夹在我的本地硬盘驱动器,并配置我的应用程序使用反射来从实现的接口(IDataManipulate.Execute)调用特定的功能。

I can create assemblies using my API, copy the file to a folder in my local hard drive and configure my application to use Reflection to call a specific function from the implemented interface (IDataManipulate.Execute).

问题:

由于应用程序将被安装在网络中的多个工作站,是不可能复制的DLL插件的用户将创建到每一台机器。

Since the application will be installed in multiple workstations in the network, is impossible to copy the plugin dlls the users will create to each machine.

解决方案我想:

解决方法1 API的DLL复制到网络共享。

Solution 1 Copy the API dll to a network share.

问题: 需要AllowPartiallyTrustedCallersAttribute程序,这需要.NET唱歌,这是我无法从我的用户强制。

Problem: Requires AllowPartiallyTrustedCallersAttribute, which requires .Net singing, which I can't force from my users.

解决方案2 (preferred) 序列化的dll对象,将其保存到数据库中,反序列化和呼叫IDataManipulate.Execute。

Solution 2 (preferred) Serialize the dll object, save it to the database, deserialize it and call IDataManipulate.Execute.

问题: 反序列化后,我尝试转换为IDataManipulate对象,但返回一个错误寻找实际的dll文件。

Problem: After deserialization, I try cast it to a IDataManipulate object but returns an error looking for the actual dll file.

解决方案3 每次用户启动我的应用程序时保存的dll字节的byte []到数据库中并重新创建DLL在本地PC上。

Solution 3 Save the dll bytes as byte[] to the database and recreate the dll at the local PC every time the user starts my application.

问题: DLL可能有相关性,我不知道如果我能察觉。

Problem: Dll may have dependencies, which I don't know if I can detect.

任何建议,将大大AP preciated。

Any suggestions will be greatly appreciated.

感谢

推荐答案

我以前做过方案三。我们存储在一个数据库表中的DLL文件与上次修改时间戳。这样,你可以告诉我们,如果本地文件所需的应用程序启动时进行更新。

I've done "Solution 3" before. We stored the DLL files in a database table with a "last modified" timestamp. That way you could tell if the local file needed to be updated when the app started.

您可以拨打 Assembly.GetReferencedAssemblies 来获得相关性列表的程序集。这假定该插件DLL不使用反射动态加载的随机组件,但应该是可接受的。

You can call Assembly.GetReferencedAssemblies to get the list of dependencies from an assembly. This assumes that the plugin DLL doesn't use reflection to dynamically load a random assembly, but that should be acceptable.

另一种选择是使用 AppDomain.AssemblyResolve 事件。相反,下载所有启动时的插件的DLL的,本次活动将让你只下载真正需要的DLL文件。

Another option is to use the AppDomain.AssemblyResolve event. Instead of downloading all of the plugin DLLs on startup, this event would let you only download the DLLs that are actually needed.