开发者

DynamicActivity - How to invoke a workflow that stored in Database?

This i开发者_如何学Gos 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.


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜