如何管理多个crm 2011工作流异步调用?

时间:2013-03-19 12:36:34

标签: dynamics-crm-2011 dynamics-crm

我正在尝试构建一个自定义工作流程,将联系人或帐户插入到营销列表中。 因此,除非我尝试拨打多个电话,否则它的效果非常好。在这种情况下,我注意到并非每个进程都有这个错误:

  

由于错误导致工作流暂停:未处理的异常:   System.ServiceModel.FaultException`1 [Microsoft.Xrm.Sdk.OrganizationServiceFault,   Microsoft.Xrm.Sdk,Version = 5.0.0.0,Culture = neutral,   PublicKeyToken = 31bf3856ad364e35]]:ValidateOpen - 遇到处理   CrmDbConnection什么时候不应该处理详细信息:   
  -2147220968消息> ValidateOpen - 遇到丢弃的CrmDbConnection时   它不应该被处理/消息>
  2013-03-19T11:47:06.588342Z   -2147220970      

消息> System.ObjectDisposedException:Microsoft Dynamics CRM具有   遇到了错误。管理员或支持的参考编号:   FB9BF845 /消息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using System.IO;

namespace ContactToMarketingList
{
    public class ContactToMList : CodeActivity
    {      
        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                new AddToList(executionContext, contact, MList, inserimento);
            }
            catch 
            {

            }
        }

        [Input("Contatto")]
        [ReferenceTarget("contact")]
        public InArgument<EntityReference> contact { get; set; }

        [Input("Marketing List")]
        [ReferenceTarget("list")]
        public InArgument<EntityReference> MList { get; set; }

        [Input("Inserimento")]
        public InArgument<bool> inserimento { get; set; }

        bool action = false;
        private static IOrganizationService myService = null;
        private static string _separatore = "\r\n";
        private static string fileName = "c:\\temp\\ContactToMList_logs.txt";

        int count = 0;
    }

    public class AddToList
    {
        private static Guid _contactiId;
        private static Guid _listId;
        private static bool _insert = false;
        private static bool _action = false;
        private static IExecutionContext _executionContext = null;
        private static IOrganizationServiceFactory _organizationServiceFactory = null;
        private static IOrganizationService _service = null;

        public AddToList(CodeActivityContext executionContext, InArgument<EntityReference> contact, InArgument<EntityReference> MList, InArgument<bool> inserimento)
        {
            _executionContext = executionContext.GetExtension<IExecutionContext>();
            _organizationServiceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            _service = _organizationServiceFactory.CreateOrganizationService(_executionContext.UserId);
            _contactiId = contact.Get<EntityReference>(executionContext).Id;
            _listId = MList.Get<EntityReference>(executionContext).Id;
            _insert = inserimento.Get<bool>(executionContext);

            _addtoList();
        }

        private static void _addtoList()
        {
            XrmDataContext datacontext = new XrmDataContext(_service);           
            var MyContact = (from c in datacontext.ContactSet where c.ContactId == _contactiId select c.Id).ToArray();            
            var MyList = (from l in datacontext.ListSet where l.Id == _listId select l).ToList().FirstOrDefault();           
            var members = (from m in datacontext.ListMemberSet where m.ListId.Id == MyList.ListId select m.EntityId.Id).ToArray();        

            foreach (Guid id in members)
                if (MyContact.FirstOrDefault() == id)
                    _action = true;

            if (_insert && !_action)
            {
                AddListMembersListRequest AddMemberRequest = new AddListMembersListRequest();

                AddMemberRequest.ListId = _listId;
                AddMemberRequest.MemberIds = MyContact;               
                AddListMembersListResponse AddMemberResponse = _service.Execute(AddMemberRequest) as AddListMembersListResponse;
            }
            else if (!_insert && _action)
            {
                //  File.AppendAllText(fileName, "Remove contact to list - INIZIO"+_separatore);
                RemoveMemberListRequest RemoveMemberRequest = new RemoveMemberListRequest();
                RemoveMemberRequest.ListId = _listId;
                RemoveMemberRequest.EntityId = MyContact.FirstOrDefault();
                // Use AddListMembersListReponse to get information about the request execution 
                RemoveMemberListResponse RemoveMemberResponse = _service.Execute(RemoveMemberRequest) as RemoveMemberListResponse;
                // service.Update(MyList);
                // File.AppendAllText(fileName, "Remove contact to list - FINE"+_separatore);   
            }
            else
            {
                // File.AppendAllText(fileName, insert == true ? "L'utente è già presente nella Lista di Marketing." +_separatore: "L'utente non è presente nella Lista di Marketing." +_separatore); 
            }    
        }
    }
}

0 个答案:

没有答案