开发者

Loading assemblies dynamically‏ AssemblyResolve issue

Note: This is a continuation of my previous post: Complicated API issue with calling assemblies dynamically‏


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

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 IDataManipulate.Execute().

I've decided to go with 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.

So this is what I'm doing:

  • My application prompts the user to upload a .Net assembly that implements my API's interface (IDataManipulate) ("main" asseembly) and save the bytes in the database.
  • Then, using Assembly.GetReferencedAssemblies I get a list of referenced assemblies and save their bytes in the database ("referenced" assemblies), assuming the user provided the actual files in the same folder.

    Question: How can I recognize the system assemblies from the Assembly.GetReferencedAssemblies list and exclude them?

  • When the app runs, I get the bytes for the main assembly from the database, create the assembly with Assembly.Load(byte[]) and use CreateInstance(type) where type is the object that implements IDataManipulate (so I can call IDataManipulate.Execute()).
  • Then I use the AppDomain.AssemblyResolve event to load the bytes of the references assemblies from the database using Aseembly.Load(byte[])

Everything works; the dlls are loaded and I can call IDataManipulate.Execute() except for this problem:

Problem

When I call IDataManipulate.Execute() from the main assembly, I get an error such as "I can't load type xxxx from this assembly". The type that can't be loaded belongs to one of the referenced assemblies not the main one.

Why this is h开发者_C百科appening?

Any suggestions?

Thanks


To answer your first question, you can check the referenced assemblies' Location, and only add it to the database if it's in the same folder.


A bit late, but you can also check the PublicKeyToken of the assembly. Three tokens exist for .net, I believe. You can find them by looking in the windows framework folder. This way, you can include assemblies in other folders if you need to.

That said, I would probably require the developer to have them in the same folder as well. In case they decide to include large 3rd party libraries like Infragistics or whatnot. That way they explicitly have to move the assemblies to a folder before importing, making them aware of what they are doing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜