从mvc控制器运行wf工作流程

时间:2015-07-20 11:51:27

标签: model-view-controller workflow-foundation

在这里输入代码我打算在由MVC Framework编写的Web应用程序中使用WF4.5。我已经使用WorkflowApplication类实例来运行我的WorkFlow。但每当我在运行实例的控制器中调用该方法时,我都会收到此错误:

  

此时无法启动异步操作。异步操作只能在异步处理程序或模块中启动,或者在页面生命周期中的某些事件中启动。如果在执行页面时发生此异常,请确保将页面标记为<%@ Page Async =" true" %取代。此异常也可能表示尝试调用" async void"方法,在ASP.NET请求处理中通常不受支持。相反,异步方法应该返回一个Task,调用者应该等待它

我写过这个可以执行工作流程的类:

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

1 个答案:

答案 0 :(得分:0)

使用WorkflowInvoker而不是WorkflowApplication。