title: How to correctly load a WF4 workflow from XAML?
body: <h2>Short version:</h2>
  
  <p>How do I load a WF4 workflow from XAML?
  Important detail: The code that loads the workflow shouldn't need to know beforehand which types are used in the workflow.</p>
  
  <hr>
  
  <h2>Long version:</h2>
  
  <p>I am having a very hard time loading a WF4 workflow from the XAML file create by Visual Studio.
  My scenario is that I want to put this file into the database to be able to modify it centrally without recompiling the Workflow invoker.</p>
  
  <p>I am currently using this code:</p>
  
  <pre><code>var xamlSchemaContext = new XamlSchemaContext(GetWFAssemblies());
  var xmlReaderSettings = new XamlXmlReaderSettings();
  xmlReaderSettings.LocalAssembly = typeof(WaitForAnySoundActivity).Assembly;
  var xamlReader = ActivityXamlServices.CreateBuilderReader(
                       new XamlXmlReader(stream, xmlReaderSettings), 
                       xamlSchemaContext);
  
  var activityBuilder = (ActivityBuilder)XamlServices.Load(xamlReader);
  var activity = activityBuilder.Implementation;
  var validationResult = ActivityValidationServices.Validate(activity);
  </code></pre>
  
  <p>This gives me a whole lot of errors, which fall into two categories:</p>
  
  <p><strong>Category 1:</strong><br>
  Types from my assemblies are not known, although I provided the correct assemblies to the constructor of <code>XamlSchemaContext</code>.</p>
  
  <blockquote>
    <p>ValidationError { Message = Compiler error(s) encountered processing expression "GreetingActivationResult.WrongPin".
    'GreetingActivationResult' is not declared. It may be inaccessible due to its protection level.
    , Source = 10: VisualBasicValue, PropertyName = , IsWarning = False }</p>
  </blockquote>
  
  <p>This can be solved by using the technique described <a href="http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/2b77771e-84a6-4ec3-a944-3de2a60201fc">here</a>, which basically adds the assemblies and namespaces of all used types to some <code>VisualBasicSettings</code> instance:</p>
  
  <pre><code>var settings = new VisualBasicSettings();
  settings.ImportReferences.Add(new VisualBasicImportReference
  {
      Assembly = typeof(GreetingActivationResult).Assembly.GetName().Name,
      Import = typeof(GreetingActivationResult).Namespace
  }); 
  // ...
  VisualBasic.SetSettings(activity, settings);
  // ... Validate here
  </code></pre>
  
  <p>This works but makes the whole "dynamic loading" part of the Workflow a joke, as the code still needs to know all used namespaces.<br>
  <strong>Question 1:</strong> Is there another way to get rid of these validation errors without the need to know beforehand which namespaces and assemblies are used?</p>
  
  <p><strong>Category 2:</strong><br>
  All my input arguments are unknown. I can see them just fine in <code>activityBuilder.Properties</code> but I still get validation errors saying they are unknown:</p>
  
  <blockquote>
    <p>ValidationError { Message = Compiler error(s) encountered processing expression
    "Pin".
    'Pin' is not declared. It may be inaccessible due to its protection level.
    , Source = 61: VisualBasicValue, PropertyName = , IsWarning = False }</p>
  </blockquote>
  
  <p>No solution so far.<br>
  <strong>Question 2:</strong> How to tell WF4 to use the arguments defined in the XAML file?</p>