我有一个简单的工作流程作为服务托管。我只想处理此工作流中的异常。
我的工作流程将"字符串"作为参数,并尝试将其转换为 int。因此,每当我发送诸如" asasafs"之类的数据时,它都会失败并引发异常。非常简单:)
我已经读到我可以创建自己的WorkflowServiceHostFactory,但不幸的是我无法完成我的简单任务,这是我的实现:
public class MyServiceHostFactory : System.ServiceModel.Activities.Activation.WorkflowServiceHostFactory
{
protected override WorkflowServiceHost CreateWorkflowServiceHost(Activity activity, Uri[] baseAddresses)
{
return base.CreateWorkflowServiceHost(activity, baseAddresses);
}
protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses)
{
var host = base.CreateWorkflowServiceHost(service, baseAddresses);
WorkflowRuntimeBehavior wrb = host.Description.Behaviors.Find<WorkflowRuntimeBehavior>();
if (wrb == null)
wrb = new WorkflowRuntimeBehavior();
wrb.WorkflowRuntime.ServicesExceptionNotHandled += WorkflowRuntime_ServicesExceptionNotHandled;
wrb.WorkflowRuntime.Started += WorkflowRuntime_Started;
wrb.WorkflowRuntime.WorkflowCompleted += WorkflowRuntime_WorkflowCompleted;
host.Description.Behaviors.RemoveAll<WorkflowRuntimeBehavior>();
host.Description.Behaviors.Add(wrb);
host.Faulted += host_Faulted;
host.UnknownMessageReceived += host_UnknownMessageReceived;
return host;
}
void workflowRuntime_WorkflowCreated(object sender, WorkflowEventArgs e)
{
throw new NotImplementedException();
}
void WorkflowRuntime_WorkflowCompleted(object sender, System.Workflow.Runtime.WorkflowCompletedEventArgs e)
{
throw new NotImplementedException();
}
void WorkflowRuntime_Started(object sender, System.Workflow.Runtime.WorkflowRuntimeEventArgs e)
{
throw new NotImplementedException();
}
void WorkflowRuntime_ServicesExceptionNotHandled(object sender, System.Workflow.Runtime.ServicesExceptionNotHandledEventArgs e)
{
throw new NotImplementedException();
}
void host_UnknownMessageReceived(object sender, System.ServiceModel.UnknownMessageReceivedEventArgs e)
{
throw new NotImplementedException();
}
void host_Faulted(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
我使用的是Visual Studio 2k10和iisexpress,每当工作流引发异常时,调试器都不会中断我的任何事件处理程序。你知道如何正确地做到这一点吗?
这真的取决于你想做什么。对于使用标准 WCF 堆栈向工作流发送 SOAP 消息的用户,使用 IErrorHandler 或消息检查器,您应该能够看到返回到客户端的错误。
然而,这只是故事的一部分。将响应发送回客户端时,不会执行工作流。相反,只要它有任何工作要做,它就会继续执行。由于这是在响应客户端发送之后,WCF 堆栈不会向您显示发生的任何错误。
相反,使用 TrackingParticipant 并检查 FaultPropagationRecord 将告诉您活动本身未处理的任何异常。它可能仍由 TryCatch 活动处理。检查 WorkflowInstanceUnhandledExceptionRecord 会告诉您异常未在工作流中处理,而是一直传播到运行时。