0

I'm trying to build a custom workflow that inserts contacts or account into a marketing list. So, it works perfectly except when i try to make multiple calls. In that case, I notice that not every process complete with this error:

Workflow paused due to error: Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: ValidateOpen - Encountered disposed CrmDbConnection when it should not be disposedDetail:
-2147220968 Message>ValidateOpen - Encountered disposed CrmDbConnection when it should not be disposed /Message>
2013-03-19T11:47:06.588342Z -2147220970

Message>System.ObjectDisposedException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: 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); 
            }    
        }
    }
}
Pedro Azevedo
  • 2,507
  • 1
  • 16
  • 22
hello B
  • 891
  • 5
  • 18
  • 41
  • Trying see [here](http://stackoverflow.com/questions/9265074/crm-2011-workflow-invalid-pointer-error) a similar question with a possible solution, the error is similar. Good luck. – Pedro Azevedo Mar 19 '13 at 13:45
  • Hi, thanks for your answer...Something is not clear for me in that post. So, he said: "when subsequent executions come to the code which sets the class-level service variable to an instance of an IOrganisationService, it finds the variables already has one and that it's open. The solution I've found easiest to implement it to have the service variable within the Execute function, rather than class-level. This has solved the problem everywhere that I've tried it since." But I didn't get how hate this variable within the Execute Method. Could make me some example??? – hello B Mar 19 '13 at 16:43

0 Answers0