运行 来自 mvc 控制器的 wf 工作流

running wf workflow from mvc controller

enter code here我打算在使用MVC Framework编写的Web应用程序中使用WF4.5。我已经将 WorkflowApplication class 实例用于 运行 我的 WorkFlow。但是每当我在控制器中调用 运行 实例的方法时,我都会收到此错误:

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

我写了这个 class 负责执行工作流:

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

使用 WorkflowInvoker 而不是 WorkflowApplication。