views:

41

answers:

1

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 happening?

Any suggestions?

Thanks

+1  A: 

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.

SLaks
That's a good idea.. never thought to use the Location property. I was looking for a way to prompt the user on not copied assemblies. Thanks.
Stefanos Tses