views:

48

answers:

2

Hi,

This is proof of concept project - The goal is to create an application that receive some system wide events and based on some business rules invokes a specific workflow.

The workflows are created separately and the xaml source is stored in a database.

Following is the code that used to invoke the workflow:

public void RaiseEvent(IEvent e, IEventData eventData)
{ 

    var typeName = e.GetType().FullName;

    // Query Db for all workflows for the event
    var repo = new WorkflowRepository();

    var workflows = repo.GetActiveWorkflowsByEvent(typeName);

    foreach (var wf in workflows)
    {

        var condition =   
 ConditionEvaluator.PrepareCondition(wf.Condition.Expression, eventData);

        var okToStart = ConditionEvaluator.Evaluate(condition);
        if (okToStart)
        {

           // Next line is throwing an exeption
            object o = XamlServices.Parse(wf.WorkflowDefinition.Expression);


            DynamicActivity da = o as DynamicActivity;

            WorkflowInvoker.Invoke(da,  
                   new Dictionary<string, object>   
                       {{ "EventData", eventData }});                   


        }

    }

We have created very simple workflow that runs without problems on its own. But when xaml is being loaded using XamlService.Parse it throw following exception:

System.Xaml.XamlObjectWriterException was unhandled
  Message='No matching constructor found on type 'System.Activities.Activity'.   
You can use the Arguments or FactoryMethod directives to construct this type.' 
Line number '1' and line position '30'.

Any idea what is wrong? Thank you.

+1  A: 

Not sure what is causing your problem, I have used XamlServices.Load() in the past without any problems, but the easiest way of loading a workflow XAML at runtime is by using the ActivityXamlServices.Load(). See here for an example.

Maurice
Thanks, That exactly what I did.I have posted the answer below
Michael D.
A: 

Ok I have solved this by using ActivityXamlServices

So Instead of this line:

object o = XamlServices.Parse(wf.WorkflowDefinition.Expression);

I am using following snippet:

var mStream = new memoryStream(
    ASCIIEncoding.Default.GetBytes(wf.WorkflowDefinition.Expression));

object o = ActivityXamlServices.Load(mStream);
Michael D.