1、从Xoml文件加载工作流时runtime.CreateWorkflow(XmlTextReader.Create("workflow.xoml");
异常信息:未处理 System.Workflow.ComponentModel.Compiler.WorkflowValidationFailedException Message="工作流验证失败。"
实质错误为:在未创建新 Activity 类型的情况下直接执行标记时不能使用 "x:Code" / "x:Class"。
处理方式为:删除x:Class/x:Code结点内容;例如下面xoml文件内容的x:Code就无法加载:
<SequentialWorkflowActivity 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 int _amount = 500; public int Amount { get { return _amount; } set { _amount = value; } } private void SayHello(object sender, EventArgs e) { Console.WriteLine("Hello, workflow!"); Console.ReadKey(); } ]]></x:Code> </SequentialWorkflowActivity>
2、本地通信服务定义的事件被客户端订阅时需要将触发事件的Sender参数必须设置为null,否则将报错如下:
无法为实例 ID“a7f482a5-dc7d-4700-b17a-7cf6c3dc8ffd”传递接口类型“OrderWorkflowLibrary.IOrderService”上的事件“OrderRequested”。
实质错误为:EventArgs 不可序列化
[ExternalDataExchange]
public interface IOrderService
{
event EventHandler<OrderEventArgs> OrderRequested; //工作流定义中HandleExternalEventActivity活动中指定的外部事件
void NotifyOrderIdled(Guid instanceId, string userId); //工作流定义中CallExternalMethodActivity活动中指定的外部方法
}
[Serializable]
public class OrderService : IOrderService
{
public event EventHandler<OrderEventArgs> OrderRequested;
public void RaiseOrderRequestedEvent(Guid instanceId, string userId)
{
if (OrderRequested != null)
{
OrderEventArgs e = new OrderEventArgs(instanceId, userId);
OrderRequested(this, e); //需要改为:OrderRequested(null, e);
}
}
public event EventHandler<OrderIdledEventArgs> OrderIdled; //被客户代码订阅的事件
public void NotifyOrderIdled(Guid instanceId, string userId)
{
if (OrderIdled != null)
{
OrderIdledEventArgs e = new OrderIdledEventArgs(instanceId, userId);
OrderIdled(this, e); //这里的事件是发布给客户代码而不是工作流故没有问题
}
}
}
public interface IOrderService
{
event EventHandler<OrderEventArgs> OrderRequested; //工作流定义中HandleExternalEventActivity活动中指定的外部事件
void NotifyOrderIdled(Guid instanceId, string userId); //工作流定义中CallExternalMethodActivity活动中指定的外部方法
}
[Serializable]
public class OrderService : IOrderService
{
public event EventHandler<OrderEventArgs> OrderRequested;
public void RaiseOrderRequestedEvent(Guid instanceId, string userId)
{
if (OrderRequested != null)
{
OrderEventArgs e = new OrderEventArgs(instanceId, userId);
OrderRequested(this, e); //需要改为:OrderRequested(null, e);
}
}
public event EventHandler<OrderIdledEventArgs> OrderIdled; //被客户代码订阅的事件
public void NotifyOrderIdled(Guid instanceId, string userId)
{
if (OrderIdled != null)
{
OrderIdledEventArgs e = new OrderIdledEventArgs(instanceId, userId);
OrderIdled(this, e); //这里的事件是发布给客户代码而不是工作流故没有问题
}
}
}
客户端调用工作流代码:
private void StartOrderWorkflow()
{
_workflowRuntime = new WorkflowRuntime();
_externalDataExchangeService = new ExternalDataExchangeService();
_workflowRuntime.AddService(_externalDataExchangeService);
_OrderService = new OrderService();
_OrderService.OrderIdled += new EventHandler<OrderIdledEventArgs>(_OrderService_OrderIdled);
_externalDataExchangeService.AddService(_OrderService);
_workflowRuntime.StartRuntime();
_workflowInstance = _workflowRuntime.CreateWorkflow(typeof(OrderWorkflow));
_workflowInstance.Start();
}
{
_workflowRuntime = new WorkflowRuntime();
_externalDataExchangeService = new ExternalDataExchangeService();
_workflowRuntime.AddService(_externalDataExchangeService);
_OrderService = new OrderService();
_OrderService.OrderIdled += new EventHandler<OrderIdledEventArgs>(_OrderService_OrderIdled);
_externalDataExchangeService.AddService(_OrderService);
_workflowRuntime.StartRuntime();
_workflowInstance = _workflowRuntime.CreateWorkflow(typeof(OrderWorkflow));
_workflowInstance.Start();
}