Windows Workflow Services - 自定义服务主机。处理工作流事件

时间:2012-10-10 13:10:40

标签: c# web-services workflow-foundation-4 workflow-foundation workflowservice

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

我的工作流将'string'作为参数,并尝试将其强制转换为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,并且每当工作流抛出异常时,调试器都不会破坏我的任何事件处理程序。 你知道怎么做得好吗?

1 个答案:

答案 0 :(得分:1)

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

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

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