开发者

How can I dynamically load and unload .Net assemblies inside a WCF service

I want to develop a windows service 开发者_Go百科hosted wcf web service that would behave as a task execution management and reporting service. Each task would be represented as a .net assembly, the task name and dll path would be in a central configuration. The task would primarily have a Execute and a Stop method at the very least. It should be possible to load, execute, stop and unload tasks while the wcf service is running and other tasks are running. One instance of each task can be running concurrently. One task having an exception should not abort other tasks. I am thinking of methods like

[OperationContract]    
bool Load(string taskName);
[OperationContract]
bool Run(string taskName);

I am thinking of how I can accomplish this loading and unloading tasks on demand as well as the isolation. Some options would be using Reflection, MAF (System.Addin) etc. The initial performance hit to load is acceptable as this load/unload would occur very infrequently.

What is the best way (library, api, framework etc) to achieve this goal.


I can not say about the best, but the ONLY (!) approach involvesm ultiple appdomains. Because you can NOT unload an assembly, only an appdomain, so all assemblies belonging to a task must be loaded in a separate appdomain.

This is technically very trivial to do. The devil is in the detail. MAF may be a good framework for that.


I would just define an interface which each supplied assembly must implement.

I'm working on a project that implements a pipeline behaviour, this code snippet might be helpful to you: it uses linq to search all loaded assemblies for classes that implement a particular interface and which have an empty constructor and a custom attribute (you may not need the custom attribute part) - and it instantiates an instance of each item found.

I appreciate that this isn't exactly what you're after, but hopefully it'll point you in the right direction - Hope it helps :)

        var processingModules = from mod in
                                   AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly =>
                                   {

                                       return from t in assembly.GetTypes()

                                              where 
                                                    t.GetInterfaces().Contains(typeof(IProcessingModule<T>))
                                                    && t.GetConstructor(Type.EmptyTypes) != null
                                                    && t.GetCustomAttributes(typeof(ProcessModuleAttribute), true)
                                                           .Cast<ProcessModuleAttribute>()
                                                           .Select(e => e.Processes.Contains(manager.Name))
                                                           .Contains(true)

                                              select Activator.CreateInstance(t) as IProcessingModule<T>;
                                   })
                                   orderby mod.ModuleOrder
                                       select mod;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜