Link to home
Start Free TrialLog in
Avatar of bvRocks
bvRocksFlag for Afghanistan

asked on

Workflow Question on Loading xoml

I have this xoml which is created dynamically using the below code

It produces a straightforward output as
<?xml version="1.0" encoding="utf-8"?><ns0:Workflow1 InitialStateName="Workflow1InitialState" x:Name="Workflow1" DynamicUpdateCondition="{x:Null}" CompletedStateName="stateActivity1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ns0="clr-namespace:WorkflowConsoleApplication1;Assembly=WorkflowConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <ns0:Workflow1.FinalState>
            <StateActivity x:Name="stateActivity1" />
      </ns0:Workflow1.FinalState>
      <ns0:Workflow1.StartState>
            <StateActivity x:Name="Workflow1InitialState">
                  <EventDrivenActivity x:Name="eventDrivenActivity1">
                        <HandleExternalEventActivity x:Name="handleExternalEventActivity1" EventName="HandleOperation" InterfaceType="{x:Type ns0:IOperation}" />
                        <SetStateActivity x:Name="setStateActivity1" TargetStateName="stateActivity1" />
                  </EventDrivenActivity>
            </StateActivity>
      </ns0:Workflow1.StartState>
</ns0:Workflow1>

The problem is when i try to reload the xoml and start the workflow, it fails with an error at the line
instance = workflowRuntime.CreateWorkflow(xr);

the error is Can not create workflow definition.  Input markup is invalid.

I am baffled at this point
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.IO;
using System.Xml;
using System.Workflow.ComponentModel.Serialization;
using System.Windows.Forms;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
using System.Workflow.ComponentModel.Compiler;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Workflow.ComponentModel.Serialization;
using System.Windows.Forms;
using System.Reflection;

namespace WorkflowConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                OperationsHandler _Ops = new OperationsHandler();
                ExternalDataExchangeService eds = new ExternalDataExchangeService();
                workflowRuntime.AddService(eds);
                eds.AddService(_Ops);
                workflowRuntime.StartRuntime();

                Workflow1 sl = new Workflow1();
                XmlWriter writer = XmlTextWriter.Create("d:\\MyData\\A2.xoml");
                WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                serializer.Serialize(writer, sl);
                writer.Close();

                XmlReader xr = XmlReader.Create("d:\\MyData\\A2.xoml");

                serializer = new WorkflowMarkupSerializer();
                Workflow1 wf1 = (Workflow1)serializer.Deserialize(xr);

                TypeProvider typeProvider = new TypeProvider(workflowRuntime);
                workflowRuntime.AddService(typeProvider);

                WorkflowInstance instance;
                try
                {
                    instance = workflowRuntime.CreateWorkflow(xr);
                }
                catch (WorkflowValidationFailedException exp)
                {
                    StringBuilder errors = new StringBuilder();
                    foreach (ValidationError error in exp.Errors)
                    {
                        errors.AppendLine(error.ToString());
                    }
                    Console.WriteLine(errors.ToString());
                } 
                
                AutoResetEvent waitHandle = new AutoResetEvent(false);
                instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication1.Workflow1));
                instance.Start();

                Params p = new Params(Guid.NewGuid());
                p.XML = "<xml>someStuff</xml>";

                _Ops.TriggerEvent(null,p);

                waitHandle.WaitOne();
            }
        }
    }

    public interface IParams
    {
        string XML {get;set;}
    }

    [Serializable]
    public class Params : ExternalDataEventArgs, IParams
    {
        public String _xml;
        public Params(Guid id) : base(id) { }
        #region IParams Members
        public string XML
        {
            get
            {
                return _xml;
            }
            set
            {
                _xml = value;
            }
        }

        #endregion
    }
    [ExternalDataExchange]
    public interface IOperation
    {
        object ExecuteOperation(IParams operationParams);
        event EventHandler<Params> HandleOperation;
    }

    [Serializable]
    public class OperationsHandler : IOperation
    {
        #region IOperation Members

        public event EventHandler<Params> HandleOperation
        {
            add
            {
                InternalOperation += value;
            }
            remove
            {
                InternalOperation -= value;
            }
        }
        public object ExecuteOperation(IParams operationParams)
        {
            return null;
        }

        public void TriggerEvent(object sender, Params p)
        {
            InternalOperation(sender, p);
        }
        private event EventHandler<Params> InternalOperation;

        #endregion
    }
}

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Reflection;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace WorkflowConsoleApplication1
{
    partial class Workflow1
    {
        public StateActivity StartState { get { return Workflow1InitialState; } set { Workflow1InitialState = value; } }
        public StateActivity FinalState { get { return stateActivity1; } set { stateActivity1 = value; } }
        #region Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCode]
        private void InitializeComponent()
        {
            this.CanModifyActivities = true;
            this.setStateActivity1 = new System.Workflow.Activities.SetStateActivity();
            this.handleExternalEventActivity1 = new System.Workflow.Activities.HandleExternalEventActivity();
            this.eventDrivenActivity1 = new System.Workflow.Activities.EventDrivenActivity();
            this.stateActivity1 = new System.Workflow.Activities.StateActivity();
            this.Workflow1InitialState = new System.Workflow.Activities.StateActivity();
            // 
            // setStateActivity1
            // 
            this.setStateActivity1.Name = "setStateActivity1";
            this.setStateActivity1.TargetStateName = "stateActivity1";
            // 
            // handleExternalEventActivity1
            // 
            this.handleExternalEventActivity1.EventName = "HandleOperation";
            this.handleExternalEventActivity1.InterfaceType = typeof(WorkflowConsoleApplication1.IOperation);
            this.handleExternalEventActivity1.Name = "handleExternalEventActivity1";
            // 
            // eventDrivenActivity1
            // 
            this.eventDrivenActivity1.Activities.Add(this.handleExternalEventActivity1);
            this.eventDrivenActivity1.Activities.Add(this.setStateActivity1);
            this.eventDrivenActivity1.Name = "eventDrivenActivity1";
            // 
            // stateActivity1
            // 
            this.stateActivity1.Name = "stateActivity1";
            // 
            // Workflow1InitialState
            // 
            this.Workflow1InitialState.Activities.Add(this.eventDrivenActivity1);
            this.Workflow1InitialState.Name = "Workflow1InitialState";
            // 
            // Workflow1
            // 
            this.Activities.Add(this.Workflow1InitialState);
            this.Activities.Add(this.stateActivity1);
            this.CompletedStateName = "stateActivity1";
            this.DynamicUpdateCondition = null;
            this.InitialStateName = "Workflow1InitialState";
            this.Name = "Workflow1";
            this.CanModifyActivities = false;

        }

        #endregion

        private SetStateActivity setStateActivity1;
        private HandleExternalEventActivity handleExternalEventActivity1;
        private EventDrivenActivity eventDrivenActivity1;
        private StateActivity stateActivity1;
        private StateActivity Workflow1InitialState;
    }
}
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace WorkflowConsoleApplication1
{
    public sealed partial class Workflow1 : StateMachineWorkflowActivity
    {
        public Workflow1()
        {
            InitializeComponent();
        }
    }

}

Open in new window

Avatar of Bardobrave
Bardobrave
Flag of Spain image

"Input markup is invalid" points at some syntax error within your xml output, you should review it to confirm that it's exactly what you think (you know, usually we expect a certain result from an operation and actually something is messing it up) and, if all is as it is intended, you can use an XML parser with it to seek possible syntax errors.

Avatar of bvRocks

ASKER

Increased the points to the max...
Avatar of bvRocks

ASKER

When i do
                instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication1.Workflow1));
                instance.Start();


It workd just fine. However i dont want to do that because the workflow would eventually be dynamic in nature containing a large bunch of activities generated dynamically.
Have you checked that your xml output is what you expect in the error case?

If it returns the expected result have you checked it's syntax?
Avatar of bvRocks

ASKER

Yes. The xml output is exactly what is expected and on deserialization it reloads loads properly also (verified by serializing it again). However workflow execution fails if i say startworkflow with the xml file as the parameter.
ASKER CERTIFIED SOLUTION
Avatar of bvRocks
bvRocks
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial