0

我有一個簡單的工作流作爲服務託管。我只是想處理這個工作流程中的異常。Windows工作流服務 - 定製服務主機。處理工作流事件

我的工作流將'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

這真的取決於你在做什麼。對於向工作流發送SOAP消息的人使用標準WCF堆棧,所以使用IErrorHandler或消息檢查器時,應該能夠看到返回給客戶端的錯誤。

但這只是故事的一部分。當響應被髮送回客戶端時,工作流程沒有完成。相反,只要它有任何工作要做,它就會繼續執行。並且,在對客戶端的響應發送之後,WCF堆棧不會向您顯示發生的任何錯誤。

取而代之,使用TrackingParticipant並檢查FaultPropagationRecord會告訴您任何在活動本身中未處理的異常。它仍可能由TryCatch活動處理。檢查WorkflowInstanceUnhandledExceptionRecord會告訴您,該異常未在工作流中處理並一直傳播到運行時。