Dynamics CRM 2013 初体验(11):Async Message and Bulk Message Execution

    今天我们来看看新系统中的消息吧,即为API。异步消息可以将作业转换为后台任务进行处理,处理机制和传统的工作流类似。这样我们就可以避免系统被长任务挂起,用户可以在这段时间内继续处理其他的事情。但可惜的是:异步任务目前只支持ImportSolutionRequest这一个API,相信随着后续Rollup的推出会改善这种情况的。

    提交后的异步任务都会在系统中进行记录,我们可以在Settings-->System Jobs下面找到它。如下图所示

Dynamics CRM 2013 初体验(11):Async Message and Bulk Message Execution_第1张图片

    异步请求的使用方法

OrganizationRequest request = new OrganizationRequest("ImportSolution");
            request["CustomizationFile"] = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\resource\\opportunity.zip");
            request["OverwriteUnmanagedCustomizations"] = true;
            request["PublishWorkflows"] = true;
            request["SkipProductUpdateDependencies"] = true;
            request["ConvertToManaged"] = false;
            request["ImportJobId"] = Guid.NewGuid();

            ExecuteAsyncRequest asyncRequest = new ExecuteAsyncRequest();
            asyncRequest.Request = request;

            var response = svc.Execute(asyncRequest);


            if (response != null)
            {
                Guid asyncJobId = (Guid)response.Results["AsyncJobId"];
                QueryExpression query = new QueryExpression("asyncoperation");
                Entity job = null;
                query.ColumnSet = new ColumnSet(true);
                query.Criteria.AddCondition(new ConditionExpression("asyncoperationid", ConditionOperator.Equal, asyncJobId));

                while (true)
                {
                    System.Threading.Thread.Sleep(1000);
                    Console.Write(".");
                    job = svc.RetrieveSingle(query);
                    if (job.GetAttributeValue<OptionSetValue>("statecode") != null && job.GetAttributeValue<OptionSetValue>("statecode").Value == 3)
                    {
                        Console.WriteLine("completed!");
                        break;
                    }


                }
            }

    消息的批量执行

           在一起的系统中,我们每调用一个API就必须向服务器请求一次。假如我想要调用100个API来完成某个事务的处理,那么我们必须和服务器进行通信100次,这样一来,业务处理的大部分时间都消耗在网络上了。我相信消息的批量执行就是解决这一类问题的银弹,我们来看看它的使用方法吧:

ExecuteMultipleRequest request2 = new ExecuteMultipleRequest()
            {
                Requests = new OrganizationRequestCollection(),
                Settings = new ExecuteMultipleSettings() { 
                 ContinueOnError=true,
                  ReturnResponses=true
                }
            };

            CreateRequest createRequest = null;
            Entity acc = null;
            for (int i = 10000; i <= 10010; i++)
            {
                acc = new Entity("account");
                acc["name"] = i.ToString();
                createRequest = new CreateRequest();
                createRequest.Target = acc;
                request2.Requests.Add(createRequest);
                

            }
            ExecuteMultipleResponse response2= svc.Execute(request2)  as ExecuteMultipleResponse;

            foreach (var item in response2.Responses)
            {
                Console.WriteLine("ok");
            }


 

你可能感兴趣的:(Dynamics CRM 2013 初体验(11):Async Message and Bulk Message Execution)