1

We've got an enterprise ASP.NET/VB.NET application that has multiple clients running on different versions (websites) of the product code.

I am trying to make a windows service that runs on our server to execute long running tasks. This service needs to run the correct version of code for each client and I had originally thought about calling a WCF Service that would be deployed as part of the web version.

The problem is that our IIS pools recycle each night and this opens up the possibility of a job getting aborted. A job that went over 24hrs would never get an opportunity to finish.

Our server is running Windows Server 2003, IIS 6.0, .NET Framework 3.5, SQL Server 2005.

Is there a way to load assemblies dynamically to use without having to restart the service?

Edit: I ended up using System.AddIn detailed in this step by step example

Nathan Smith
  • 36,807
  • 6
  • 28
  • 25
  • Did you find a solution to your question? – Frank Hale Mar 15 '11 at 16:12
  • I've decided to use the System.AddIn Framework classes because it handles using different versions of the same assembly neatly. I was running into trouble creating AppDomains and trying to load assemblies and their dependencies into the correct context. – Nathan Smith Mar 21 '11 at 03:30
  • Sweet, I need to look into that. I didn't know it was out there. =) – Frank Hale Mar 21 '11 at 13:42

2 Answers2

1

Is there a way to load assemblies dynamically to use without having to restart the service?

Yes, there is. I suppose you have one or more types that have to be instantiated. If this is your case consider using something like this:

object instance = Activator.CreateInstance("MyAssembly.dll", "MyType");
instance.GetType().InvokeMethod(etc);

If you are running under .NET Framework 4.0 you could make use of DLR:

dynamic instance = Activator.CreateInstance("MyAssembly.dll", "MyType");
instance.MyMethod();

EDIT: Alternatively you could take a look at MEF (it is part of .NET Framework 4.0).

MEF presents a simple solution for the runtime extensibility problem.

Basically (I'm simplifying a lot) you can specify which folder has to be chosen for taking up the assemblies and then MEF will automatically instantiate the objects that, for example, share the same interface.

Shared Interface:

public interface IPlugin
{
    void ExecuteTask();
}

Plugin (external assembly):

[Export(typeof(IPlugin)]
public class Plugin1 : IPlugin
{
    public void ExecuteTask()
    {
        // Do something
    }
}

Main Application:

[Import]
private IPlugin plugin;

// Somewhere else
private void Initialize()
{
   var directory = new DirectoryCatalog("myDir");
   var container = new CompositionContainer(directory);
   container.ComposeParts(this);
}
as-cii
  • 12,819
  • 4
  • 41
  • 43
  • This worked nicely until I had to start loading the assemblies into other Application Domains. I checked out MEF but MAF seemed more appropriate to my situation. Refer [link](http://stackoverflow.com/questions/835182/choosing-between-mef-and-maf-system-addin) – Nathan Smith Mar 21 '11 at 03:43
0

Is there a way to load assemblies dynamically to use without having to restart the service?

Perhaps you could design a plugin architecture for your service and then load the different plugins to perform different tasks.

Frank Hale
  • 1,886
  • 1
  • 17
  • 31