2015-07-20 63 views
0

在此輸入代碼我打算在由MVC框架編寫的web應用程序中使用WF4.5。我用WorkflowApplication類實例來運行我的WorkFlow。但每當我調用該方法在運行的實例控制器我得到這個錯誤:從mvc控制器運行wf工作流程

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it

我寫了這個類,這是resposnsible執行工作流程:

public class WorkFlowsPipeline : IWorkFlowsPipeline 
    { 
     private IUnitOfWork _unitOfWork; 
     private SqlWorkflowInstanceStore _instanceStore; 

     public WorkFlowsPipeline(IUnitOfWork unitOfWork) 
     { 
      _unitOfWork = unitOfWork; 
      //workflowInstanceStore 
      _instanceStore = new SqlWorkflowInstanceStore(); 
      _instanceStore.ConnectionString ="data source=.;initial catalog=WFPersist;user id=sa;password=1;"; 
     } 

     public void RecordPersistedInstanceForTheUser(int userId,Guid instanceId, Models.Enums.WorkFlowTypeEnum workFlowType) 
     { 
      _unitOfWork.UsersWorkFlows.Add(new UsersWorkFlowsInstance 
      { 
       UserId = userId, 
       WorkFlowId=instanceId, 
       WorkFlowType = workFlowType 
      }); 
     } 


     public void RunCompleteProfileForUser(int userId) 
     { 
      var usersWorkFlow = _unitOfWork.UsersWorkFlows.GetAll().FirstOrDefault(x => x.UserId == userId); 
      if (usersWorkFlow == null) 
      { 
       Activity rentalWorkflow = new Activity1(); 

       Dictionary<string, object> wfArg = new Dictionary<string, object>() 
       { 
        { 
         "UOW", _unitOfWork 
        }, 
        { 
         "UserId",userId 
        } 
       }; 

       var _wfApp = new WorkflowApplication(rentalWorkflow, wfArg); 


       _wfApp.SynchronizationContext = SynchronizationContext.Current; 
       _wfApp.InstanceStore = _instanceStore; 
       //_wfApp.Extensions.Add(this); 

       var instanceId=_wfApp.Id; 
       _wfApp.Run(); 

       RecordPersistedInstanceForTheUser(userId, instanceId,WorkFlowTypeEnum.CompleteProfile); 


      } 
      else 
      { 
       //get id of instance load it from database and run it 
      } 
     } 


    } 

,我在調用的方法我控制器以這種方式操作:

public async Task<ActionResult> Index() 
     { 

      var userId = User.Identity.GetUserId<int>(); 

      _workFlowsPipeline.RunCompleteProfileForUser(userId); 

      return View(); 
     } 
+0

請將您的代碼發送至 – ojf

+0

我已經發布了我的代碼。這很簡單,我沒有看到任何特別的東西。我真的懷疑ASP.net MVC是否能夠執行WF。我看到一個使用WF使用MVC但使用AsyncControllers的例子。我想知道是否需要這樣做?我在我的動作中使用了流行的異步等待關鍵字,但沒有奏效。可能是我應該使用WCF服務,以便讓WF進入我的MVC項目儘管我的項目架構不是SOA。請幫我找到我的路。 –

+0

其實我想在MVC框架中使用WF的文檔和例子更多。有時候我認爲WF是一種已經不在現實世界中使用的死技術。 –

回答

0

使用WorkflowInvoker而不是WorkflowApplication。

+0

謝謝。但正如我所知WorkflowInvoker如此有限,以至於我們甚至無法堅持使用它。 –

+0

這是正確的。但是,如果您需要使用WorkflowApplication,則必須重新構建解決方案。我想最簡單的方法就是WorklfowServices。 – Enes