WF实例学习笔记:(2)通过Workflow 调用 WCF Data Services 获取数据

运行程序首页的效果如图所示:

首先打开HomeController.cs 文件

        public ActionResult Index()
        {
            #region XAML

            //var activity =
            //    ActivityXamlServices.Load(
            //    Server.MapPath("~/workflows/GetDataForMainPage.xaml"));
            //var results = WorkflowInvoker.Invoke(activity);
            #endregion

            var results =
                WorkflowInvoker.Invoke(
                new CustomActivities.GetDataForMainPage());

            ViewData["candidates"] = results["candidates"];
            ViewData["workOrders"] = results["workOrders"];

            return View();
        }

程序使用了WorkflowInvoker.Invoke 调用了GetDataForMainPage Activity 并将返回结果保存在ViewData中。
接着分析GetDataForMainPage Activity

并行工作流中包含了两个GetOData Activity,并将泛型Activity的返回结果IEnumerable<JobCandidate>与IEnumerable<WorkOrder>和绑定到输出参数candidate与workOrders

接下来看GetOData的实现

    public sealed class GetOData<T> : AsyncCodeActivity<IEnumerable<T>>
    {
        [RequiredArgument]
        public InArgument<Uri> ServiceUrl { get; set; }
        public InArgument<string> EntitySetName { get; set; }

        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, 
            AsyncCallback callback, object state)
        {
             DataServiceContext ctx = 
               new DataServiceContext(ServiceUrl.Get(context));

           DataServiceQuery<T> query = ctx.CreateQuery<T>(EntitySetName.Get(context));
           context.UserState = query;
           return query.BeginExecute(callback, state);
        }

        protected override IEnumerable<T> EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
        {
            DataServiceQuery<T> query = context.UserState as DataServiceQuery<T>;
            return query.EndExecute(result);
        }
        
    }

该段代码通过异步DataServiceQuery调用了WCF Data Services。

posted @ 2012-05-05 15:24  我是小马  阅读(485)  评论(0编辑  收藏  举报