开发者

How can I run multiple versions of code from a single windows service in .NET?

开发者 https://www.devze.com 2023-02-16 09:18 出处:网络
We\'ve got an enterprise ASP.NET/VB.NET application that has multiple clients running on different versions (websites) of the product code.

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 开发者_C百科restart the service?

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


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);
}


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.

0

精彩评论

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

关注公众号