0

我正在尝试构建将联系人或帐户插入营销列表的自定义工作流程。所以,它工作得很好,除非我尝试拨打多个电话。在这种情况下,我注意到并非每个过程都出现此错误:

工作流程因错误而暂停:未处理的异常: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 /Message>
2013-03-19T11:47:06.588342Z -2147220970

Message>System.ObjectDisposedException:Microsoft Dynamics CRM 遇到错误。管理员或支持参考编号:FB9BF845 /Message

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

0 回答 0