0

我将非常简单的工作流程作为服务托管。我只想处理此工作流程中的异常。

我的工作流程将“字符串”作为参数并尝试将其转换为 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,每当工作流引发异常时,调试器都不会中断我的任何事件处理程序。你知道如何正确地做吗?

4

1 回答 1

1

这真的取决于你想要做什么。对于将 SOAP 消息发送到工作流的人来说,使用标准 WCF 堆栈,因此使用 IErrorHandler 或消息检查器,您应该能够看到返回给客户端的错误。

然而,这只是故事的一部分。将响应发送回客户端时,工作流并未完成。相反,只要它有任何工作要做,它就会继续执行。因为那是在对客户端的响应发送之后,WCF 堆栈不会向您显示发生的任何错误。

相反,使用 TrackingParticipant 并检查 FaultPropagationRecord 会告诉您活动本身未处理的任何异常。它可能仍由 TryCatch 活动处理。检查 WorkflowInstanceUnhandledExceptionRecord 会告诉您异常未在工作流中处理并一直传播到运行时。

于 2012-10-10T15:10:39.500 回答