locked
Error compiling workflows at runtime! RRS feed

  • Question

  • Hi,

    I created some activities with rehosting the WF designer, and save the Xaml in a file.
    I also want to use that workflows as custom activities, I tried to compile it in order to get a type that can be used to create reusable activities.

    Code sample:

    private Type compileWorkflow(string xaml, string workflowName)
    {
                    WorkflowCompiler compiler = new WorkflowCompiler();
                    WorkflowCompilerParameters parameters = new WorkflowCompilerParameters();
                    parameters.GenerateInMemory = true;
     
                    WorkflowCompilerResults results = compiler.Compile(parameters, new string[] { @"c:\Activity.xaml" });
     
                    if (results.Errors.Count > 0)
                    {
                                   foreach (CompilerError error in results.Errors)
                                   {
                                                   Console.WriteLine(error.ErrorText);
                                   }
                                   return null;
                    }
                    return results.CompiledAssembly.GetType(workflowName);
    }


    But, I’m getting some errors when I try to compile, like:

    “A namespace cannot directly contain members such as fields or methods”

    Used activity:

    <Activity mc:Ignorable="sap"
                       x:Class="Activity"
                       mva:VisualBasic.Settings="Assembly references and imported namespaces serialized as XML namespaces"
                       xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
                       xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
                       xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities"
                       xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"
                       xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
                       xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
                       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Sequence>
        <sap:WorkflowViewStateService.ViewState>
          <scg:Dictionary x:TypeArguments="x:String, x:Object">
             <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
      </Sequence>
    </Activity>
     

    Thanks in advance,

    H.N.
    Tuesday, November 10, 2009 4:21 PM

Answers

  • I dont think it is possible to do exactly what you want. DynamicActivity is the way to go to create an Activity on the fly from a XAML document. However, as you say, only compiled activities are in the toolbox. I was thinking if you could define a custom activity for your toolbox which has a property pointing to a XAML file path, and when you drop that activity, you essentially create a type on the fly corresponding to your XAML document. Would that work?


    Senior Lead Program Manager, Windows Workflow Foundation http://blogs.msdn.com/kavitak
    Thursday, November 12, 2009 5:41 PM

All replies

  • Hi,

    WorkflowCompiler class is for workflow 3.0/3.5. You can use ActivityXamlServices.Load(string filename) to read 4.0 XAML file and create Activity instance to run or to composite your activity tree.

    Thanks,
    Jian
    Thursday, November 12, 2009 6:37 AM
  • Thanks Jian for your response. What you are suggesting me works fine if I just want to load and run an activity from xaml.
    My scenario is a little different, I’m rehosting the WF designer and storing the activities created (xaml) in a repository in order to load them into the Toolbox as custom activities, so that other users can use them.
    I don’t want to load just one file, my code in the previous post was just a sample and I didn’t explain myself.

    I found out another thread with a similar problem:
    http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/5285db2e-12b3-4512-9127-7662d48c5c2a

    and according to Matt Winkler – MSFT, I cannot use Dynamic Activity in the toolbox, and I need a compiled type to use in another xaml file:

    To create a reusable activity, you would need to compile so that you have a type, and can subsequently create instances of the type.

    This is what I really need, can someone help me find out how to compile workflows 4.0 (xaml) in runtime.

    Thanks,
    H.N.
    Thursday, November 12, 2009 11:21 AM
  • I dont think it is possible to do exactly what you want. DynamicActivity is the way to go to create an Activity on the fly from a XAML document. However, as you say, only compiled activities are in the toolbox. I was thinking if you could define a custom activity for your toolbox which has a property pointing to a XAML file path, and when you drop that activity, you essentially create a type on the fly corresponding to your XAML document. Would that work?


    Senior Lead Program Manager, Windows Workflow Foundation http://blogs.msdn.com/kavitak
    Thursday, November 12, 2009 5:41 PM
  • You can also use msbuild.exe to build a solution, which contains your XAML files. Then you can find the types in the assemblies.

    Hope it works for you,
    Thanks,
    Jian

    Friday, November 13, 2009 5:43 AM
  • @Jian: Is msbuild.exe  the only way to compile workflows(XAML)?
    @H.N.: Finally,how did you compile workflow(XAML)? Even,I had similar kind of requirement.

    Thursday, November 19, 2009 1:37 PM