views:

40

answers:

3

Hi, I'd like to create a Window Service that can load several DLLs from a certain location and publish them using Remoting on a specific TCP port (let's say 9000).

Each DLL contains a single class that will published.

For example (Test.dll)

namespace Test
{
  class Service
  {
    // methods here
  }
}

The service should publish it using Remoting tcp://<servername>:9000/Test.dll

I know how to use Remoting but I'd like to know how to implement the service so that it can load the DLLs dynamically when starting and unload them when stopping.

May be there's another way to do this?

A: 

If you are looking for a robust solution, look into MEF, the Managed Extensibility Framework.

If, however, you just want to load the DLLs as simple plugins, I have some code samples from my TournamentApi:

/// <summary>
/// Provides methods to load plugins from external assemblies.
/// </summary>
public static class PluginLoader
{
    ...

    /// <summary>
    /// Loads the plugins from a specified assembly.
    /// </summary>
    /// <param name="assembly">The assembly from which to load.</param>
    /// <returns>The plugin factories contained in the assembly, if the load was successful; null, otherwise.</returns>
    public static IEnumerable<IPluginFactory> LoadPlugins(Assembly assembly)
    {
        var factories = new List<IPluginFactory>();

        try
        {
            foreach (var type in assembly.GetTypes())
            {
                IPluginEnumerator instance = null;

                if (type.GetInterface("IPluginEnumerator") != null)
                {
                    instance = (IPluginEnumerator)Activator.CreateInstance(type);
                }

                if (instance != null)
                {
                    factories.AddRange(instance.EnumerateFactories());
                }
            }
        }
        catch (SecurityException ex)
        {
            throw new LoadPluginsFailureException("Loading of plugins failed.  Check the inner exception for more details.", ex);
        }
        catch (ReflectionTypeLoadException ex)
        {
            throw new LoadPluginsFailureException("Loading of plugins failed.  Check the inner exception for more details.", ex);
        }

        return factories.AsReadOnly();
    }
}

This takes a loaded assembly and instantiates an instance of each IPluginEnumerator in the assembly and has it return each IPluginFactory (Abstract Factory) that it supports.

Please feel free to take a look at the source code to the TournamentApi project for the rest of the code.

John Gietzen
A: 

You can use the many methods in the Assembly class to achieve this, they're loaded into the AppDomain which will be unloaded with the service when you stop it.

Check out: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfile.aspx

Lloyd
A: 

You can easily load assemblies in the .NET framework. Take a look at the Assembly class' Load method for guidance on how to do this:

http://msdn.microsoft.com/en-us/library/xbe1wdx9%28v=VS.71%29.aspx

Joel Martinez