سؤال

I have veeeery simple workflow hosted as a service. I just want is to handle exceptions from this workflow.

My workflow gets 'string' as a parameter and tries to cast it to int. So whenever I send data like 'asasafs' it fails and throws exception. Very simple :)

I've read that I can create my own WorkflowServiceHostFactory, but unfortunatelly I can't accomplish my simple task, this is my implementation:

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();
    }
} 

I'm using Visual Studio 2k10 and iisexpress, and whenever workflow throws an exception, debugger do not break in any of my event handlers. Do you know how to do it properly ?

هل كانت مفيدة؟

المحلول

It really depends on what you are are trying to do. For one sending a SOAP message to a workflow uses the standard WCF stack so with an IErrorHandler or a message inspector you should be able to see errors being returned to the client.

However that is only part of the story. A workflow is not done when the response is send back to the client. Instead it continues executing for as long as it has any work to do. And as that is after the response to the client is send the WCF stack is not going to show you any errors that occurred.

Instead using a TrackingParticipant and checking for a FaultPropagationRecord will tell you about any exception that is not handled in an activity itself. It might still be handled by a TryCatch activity. Checking for a WorkflowInstanceUnhandledExceptionRecord tells you that the exception was not handled in the workflow and propagated all the way to the runtime.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top