locked
CodeActivity

    Question

  • I've a problem with code activity. I have this xoml file..

    <SequentialWorkflowActivity x:Class="foo.Workflow1" x:Name="Workflow1" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
        <CodeActivity x:Name="codeActivity1" ExecuteCode="ExecuteAction" />
        <DelayActivity TimeoutDuration="00:00:00" x:Name="delayActivity1" />
        <ConditionedActivityGroup x:Name="conditionedActivityGroup1" />
    </SequentialWorkflowActivity>

    and code beside file has the following code..

    namespace foo
    {
        using System;
        using System.ComponentModel;
        using System.ComponentModel.Design;
        using System.Workflow.ComponentModel.Design;
        using System.Workflow.ComponentModel;
        using System.Workflow.ComponentModel.Serialization;
        using System.Workflow.ComponentModel.Compiler;
        using System.Drawing;
        using System.Collections;
        using System.Workflow.Activities;
        using System.Workflow.Runtime;
       
       
        public partial class Workflow1 : System.Workflow.Activities.SequentialWorkflowActivity
        {
            int i = 0;
            public void ExecuteAction()
            {
                System.Console.WriteLine("Hello world");
                System.Console.ReadLine();
            }
        }
    }

    but the workflow is not getting compiled and workflow is not getting executed.. I can't trace out the problem.. Please help me..
    Dheena
    Thursday, May 28, 2009 4:56 AM

Answers

  • The Compile() method returns an instance of WorkflowCompilerResults, which contains a property named "CompiledAssembly."  You need to get the workflow type from there using reflection, and call CreateWorkflow(workflowType):

    WorkflowCompiler compiler = new WorkflowCompiler();
    WorkflowCompilerResults results = compiler.Compile(compilerParameters, @"WorkflowConsoleApplication1\Workflow1.cs", @"WorkflowConsoleApplication1\Workflow1.designer.cs");
    
    Assembly workflowAssembly = results.CompiledAssembly;
    Type workflowType = workflowAssembly.GetType("WorkflowConsoleApplication1.Workflow1");
    
    WorkflowInstance instance = workflowRuntime.CreateWorkflow(workflowType);
    instance.Start();
    

     


    Angel Azcarraga | Test Manager | Windows Workflow Foundation
    • Marked as answer by theH3llboy Friday, May 29, 2009 4:37 AM
    • Unmarked as answer by theH3llboy Friday, May 29, 2009 9:29 AM
    • Marked as answer by theH3llboy Friday, May 29, 2009 9:32 AM
    Thursday, May 28, 2009 4:43 PM

All replies

  • Can you please provide some details on how you are trying to compile the workflow and what errors you are getting?
    Angel Azcarraga | Test Manager | Windows Workflow Foundation
    Thursday, May 28, 2009 5:16 AM
  • A change i made to code beside file

    namespace foo
    {
        using System;
        using System.ComponentModel;
        using System.ComponentModel.Design;
        using System.Workflow.ComponentModel.Design;
        using System.Workflow.ComponentModel;
        using System.Workflow.ComponentModel.Serialization;
        using System.Workflow.ComponentModel.Compiler;
        using System.Drawing;
        using System.Collections;
        using System.Workflow.Activities;
        using System.Workflow.Runtime;
       
       
        public partial class Workflow1 : System.Workflow.Activities.SequentialWorkflowActivity
        {
            int i = 0;
            public void ExecuteAction(object sender, System.EventArgs e )
            {
                System.Console.WriteLine("Hello world");
                System.Console.ReadLine();
            }
        }
    }


    now the workflow is getting compiled and workflow is not getting executed..
    the error Message = "The workflow failed validation."

    I use the following code to run the workflow

    XmlReader reader = XmlReader.Create(xoml);

    WorkflowInstance instance = workflowRuntime.CreateWorkflow(reader);

    instance.Start();


    Dheena
    Thursday, May 28, 2009 5:31 AM
  • You cannot use the CreateWorkflow(XmlReader) method to run your workflow because it has code beside.  Workflows with code beside must be compiled before execution.  The CreateWorkflow(XmlReader) overload on WorkflowRuntime can only be used for pure XAML workflows (no code beside or nested code in XAML).

    To compile your workflows, you can either use Visual Studio and compile your workflow project, or you can use the WorkflowCompiler class to programmatically compile your workflows.  Please refer to the WorkflowCompiler documentation for more info on this:  http://msdn.microsoft.com/en-us/library/system.workflow.componentmodel.compiler.workflowcompiler.aspx
    Angel Azcarraga | Test Manager | Windows Workflow Foundation
    Thursday, May 28, 2009 5:42 AM
  • I used workflow compiler class to compile the workflow..
    But how to run the workflow in this case...
    Dheena
    Thursday, May 28, 2009 6:16 AM
  • The Compile() method returns an instance of WorkflowCompilerResults, which contains a property named "CompiledAssembly."  You need to get the workflow type from there using reflection, and call CreateWorkflow(workflowType):

    WorkflowCompiler compiler = new WorkflowCompiler();
    WorkflowCompilerResults results = compiler.Compile(compilerParameters, @"WorkflowConsoleApplication1\Workflow1.cs", @"WorkflowConsoleApplication1\Workflow1.designer.cs");
    
    Assembly workflowAssembly = results.CompiledAssembly;
    Type workflowType = workflowAssembly.GetType("WorkflowConsoleApplication1.Workflow1");
    
    WorkflowInstance instance = workflowRuntime.CreateWorkflow(workflowType);
    instance.Start();
    

     


    Angel Azcarraga | Test Manager | Windows Workflow Foundation
    • Marked as answer by theH3llboy Friday, May 29, 2009 4:37 AM
    • Unmarked as answer by theH3llboy Friday, May 29, 2009 9:29 AM
    • Marked as answer by theH3llboy Friday, May 29, 2009 9:32 AM
    Thursday, May 28, 2009 4:43 PM
  • thanks man..
    Dheena
    Friday, May 29, 2009 4:36 AM
  • Angel.. i got one more problem..

    <SequentialWorkflowActivity x:Class="XamlCodeExamples.SampleWorkflow" x:Name="SampleWorkflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
        <CodeActivity x:Name="codeActivity1" ExecuteCode="SayHello" />
        <x:Code><![CDATA[
        private void SayHello(object sender, EventArgs e)
        {                                               
            Console.WriteLine("Hello World");
            Console.ReadLine();
        }]]></x:Code>
    </SequentialWorkflowActivity>

    i use this xoml.. I compile and run the workflow from the compiled assembly but Hello world is not getting displayed in console.. why it is not displayed

    Dheena
    Friday, May 29, 2009 9:33 AM