Full code:
using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
namespace ManyAssemblies {
class Program {
public sealed class SayHelloActivity : Activity {
readonly WriteLine writeLine = new WriteLine() { Text = "Hello Workflow 4" };
public SayHelloActivity() {
Implementation = () => { return writeLine; };
}
}
static void Main(string[] args) {
var wfDefinition = new SayHelloActivity();
for (int i = 0; i < 100; i++) {
WorkflowInvoker.Invoke(wfDefinition);
}
var allLoadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
int wfAssemblesCount = allLoadedAssemblies
.Where(a => a.FullName ==
"Workflow, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null").Count();
Console.WriteLine(string.Format("There are {0} workflow assemblies in app domain.", wfAssemblesCount));
Console.ReadLine();
}
}
}
The output from this code:
...
Hello Workflow 4
Hello Workflow 4
Hello Workflow 4
There are 100 workflow assemblies in app domain.
Every execution of wf definition creates new dynamic assembly that is loaded in current app domain. Why? Definition is always the same so this is not necessary, IMO. Is this a design flaw in WF4? Can this behavior be controlled?
I'm building a host for workflow instances. And this host must be up and running for long time, like months. Will I end up with app domain with many thousands of dynamic assemblies, even if there are only just few workflow definitions? Will this kill my performance and use up my memory?