0

Running a series of delegates with Parallel.Invoke containing calls to another a...

 2 years ago
source link: https://www.codesd.com/item/running-a-series-of-delegates-with-parallel-invoke-containing-calls-to-another-appdomain.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Running a series of delegates with Parallel.Invoke containing calls to another appdomain

advertisements

Okay I'll try and keep this short :)

Essentially what I've got is an architecture that creates a new appdomain and loads plugin assemblies into it. This is so that plugin DLLs can be hot-swappable using Shadow Copy without bringing the service offline.

I've then got a dictionary containing references to those loaded plugins (really proxy objects generated by using MarshallByRef)

When work comes into the service it will generally require several of the plugins to process the workloads that are coming in. I want to spin up a new thread for each of the plugins and feed it the workload, so that work can be processed in parallel (I'm using TPL to achieve this)

List<Action> actions = new List<Action>();
foreach(var work as workitemgroup){
    actions.Add(delegate() {
        PluginManager.Dispatchers[work.workType.ToLower()].Execute(work.workload);
    });
}
Parallel.Invoke(actions.ToArray());

The problem is this (namespace names changed to protect the innocent ;)

Type 'System.Linq.Lookup`2+Grouping[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[App.Model.Work.Workload, App.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' in Assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

strangely if I change the delegate to something like:

actions.Add(delegate() { Console.WriteLine("test"); }

...it works fine, but my question is this - what requires serialization in this case? AFAIK the delegate shouldn't need to be serialized and sent to the other AppDomain - it should be executing here and then the call to the proxy would require serialization of the workload model (which is marked as serializable anyway).


I can only guess that you use some objects created locally in the action delegates you pass to the remote plugins and these objects are not serializable. With Console.Writeline you dont pass any nonserializable object to the plugin that's why it works.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK