Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include dynamic library from web

Tags:

c#

.net

azure

I am developing a project and I have many sub modules. When I create another sub module we have to compile the project and deployment it again. That is not very cool...

I would like to create some logic for this to automatize my work. My idea is to upload the sub module to an Azure blob storage and after this my project downloads the sub module and includes this sub module in my project, without compiling the entire project.

I don't know how to make the integration with my existing software.

How can I do this?

like image 505
Taras Kovalenko Avatar asked Nov 09 '22 16:11

Taras Kovalenko


1 Answers

You have a few options. The easiest one in my opinion is to create a generic interface, something like IPlugin. Implement that in your remote assembly, and let your application check for it.

IPluginManager manager = this; // let this class implement IPluginManager

Assembly assembly = Assembly.LoadFile(downloadedFilePath);

foreach (Type type in assembly.GetTypes())
{
    foreach (Type interfaceType in type.FindInterfaces
                                   ( delegate(Type m, object filterCriteria)
                                     {
                                         return m.FullName == typeof(IPlugin).FullName;
                                     }
                                   , null
                                   )

            )
    {
        IPlugin plugin = (IPlugin)Activator.CreateInstance(interfaceType);
        plugin.Activate(manager);
    }
}

The code for IPlugin and IPluginManager:

public interface IPluginManager
{
    void Foo();
}

public interface IPlugin
{
    void Activate(IPluginManager manager);
}

This code loads a downloaded assembly saved in a file at downloadedFilePath. Then it finds the classes implementing IPlugin, and loads them.

From there on, it is all yours. You can expend both interfaces to add functionality in the plugins and the application. The class implementing IPluginManager handles all communication from and to the plugins.


If you don't like rolling your own, you could use MEF (the Managed Extensibility Framework).

like image 76
Patrick Hofman Avatar answered Nov 15 '22 06:11

Patrick Hofman